I have a system for going back to a previously-visited page in a Python console application; it works well enough, but I feel that there could be a prettier way of doing it. Does anyone have any suggestions to improve what I have below?
def main():
try:
while True:
print('Main Menu\n')
option = input('page 1, page 2, quit\n')
if option in ['1', 'one']:
menu_stack.append(0)
page_one()
elif option in ['2', 'two']:
menu_stack.append(0)
page_two()
elif option in ['q', 'quit']:
quit()
else:
print('Invalid input, please try again\n')
sleep(1)
except KeyboardInterrupt:
quit()
Very similar to the main menu page, below is page 1:
def page_one():
while True:
clear_terminal()
print('Page One\n')
option = input('Page 2, back\n')
if option in ['2', 'two']:
menu_stack.append(1)
page_two()
elif option in ['b', 'back']:
menu_checker()
else:
print('Invalid input, please try again\n')
sleep(1)
menu_checker()
calls the other pages based on what pops from the stack:
def menu_checker():
page = menu_stack.pop()
if page == 1:
page_one()
elif page == 2:
page_two()
elif page == 0:
main()
else:
print('Error')
Does anyone have any better ideas/solutions? Even though it doesn't cause any issues (as far as I am aware), I feel what I have is kind of clunky and could be improved.
An idea I had was to stack function calls; so the popped values of menu_stack() would be functions, not integers. The issue with this is that I don't know how to do that, or even if it can be done.
\$\endgroup\$