-2

I have tried to find what I am seeking before posting this. But I have a hard time formulating the question and finding an answer.

I wonder if there is any way of having a key, such as "b", that takes the user back to the main menu where ever he is while running my program. I have a menu and sub-menus and I want the user to be able to go back to the menu wherever he is by just pressing "b". I wonder if there is any easy way of doing this instead of putting

if choice= b:
    menu()

whenever I have an input()...

I hope this is not too confusing! Would really appreciate a answer!

tobias_k
  • 81,265
  • 12
  • 120
  • 179
Krister
  • 1
  • 1
  • Are you talking about command line program, or a graphical one? If it's graphical, then what library are you using? – Tomasz Nocoń Nov 17 '16 at 22:19
  • sorry, it is very confusing to me... by _meny_ do you mean _menu_? If the user could go to `meny` at any time what would you expect to happen with what ever is currently happening in the program? – Tadhg McDonald-Jensen Nov 17 '16 at 22:24
  • This might be useful http://stackoverflow.com/a/39269807/5811078 While it is workaround, it sure does work. – zipa Nov 17 '16 at 22:25

1 Answers1

0

Not recommended for making your code easy to read but...

You could make your 'b' input check a function, then run that function at the start of each input check

def b_check(option):
    if option == 'b':
        main_menu()
    else:
        return option

def main_menu():
    #your main menu function goes here

#your active menu code goes here

#ask the user to make their selection
#Note: for Python 2.x use `raw_input`
option = input('Enter your choice')

b_check(option)
if option == 'a':
    #do this
elif option =='c':
    #do that
CJC
  • 400
  • 4
  • 15