I have the following code for the main menu:
>>> def mainmenu ():
d = ''
while d == '':
print ('\nM A I N M E N U')
print ('1. Settings')
print ('q. Quit')
option = input ('Select an option: ')
if option.lower () == 'q':
sys.exit ()
elif option == '1':
d = submenu ()
else:
print ('Invalid selection!')
return d
>>>
Submenu Code:
>>> def submenu ():
p = ''
while p == '':
print ('\nS U B M E N U')
print ('1. Perform an action')
print ('b. Back')
print ('q. Quit')
option = input ('Select an option: ')
if option.lower () == 'q':
sys.exit ()
elif option.lower () == 'b':
mainmenu ()
return
elif option == '1':
p = 'Do something'
else:
print ('Invalid selection!')
return p
>>>
Whenever I issue a "back" command from the submenu to go back to the mainmenu, the whole menu system broke. It appeared that the parent menu continued where it left off calling another submenu instance where
p = ''
which caused value to be empty.
Here's an example of the said error:
>>> value = mainmenu ()
M A I N M E N U
1. Settings
q. Quit
Select an option: 1
S U B M E N U
1. Perform an action
b. Back
q. Quit
Select an option: b
M A I N M E N U
1. Settings
q. Quit
Select an option: 1
S U B M E N U
1. Perform an action
b. Back
q. Quit
Select an option: 1
>>>
>>> value
>>>
However, if I select "option 1" from submenu and do not use the "back" option, everything works as expected.
>>> value = mainmenu ()
M A I N M E N U
1. Settings
q. Quit
Select an option: 1
S U B M E N U
1. Perform an action
b. Back
q. Quit
Select an option: 1
>>>
>>> value
'Do something'
>>>
How do I fix the "back" option feature?
I've attempted to insert a return statement under the option 1 for main menu and every where that I could think of but it didn't work. I'm lacking some logic and any explanation or help is very much appreciated.
...
elif option == '1':
d = submenu ()
return
...
Update 1
- Added a variable "msg" to mainmenu under option == '1' and return a tuple.
def mainmenu ():
d = ''
msg = '' # Added in Update #1
while d == '':
print ('\nM A I N M E N U')
print ('1. Settings')
print ('q. Quit')
option = input ('Select an option: ')
if option.lower () == 'q':
sys.exit ()
elif option == '1':
msg = 'Option 1' # Added in Update #1
d = submenu ()
else:
print ('Invalid selection!')
return msg, d # Modified in Update #1
- Unpacked and printed the tuple in main ()
message, action = mainmenu ()
print ('\nMessage: ', message)
print ('Action: ', action)
The answer below works but introduced another problem when the option 'b' is selected.
M A I N M E N U
1. Settings
q. Quit
Select an option: 1
S U B M E N U
1. Perform an action
b. Back
q. Quit
Select an option: b
M A I N M E N U
1. Settings
q. Quit
Select an option: 1
S U B M E N U
1. Perform an action
b. Back
q. Quit
Select an option: 1
Message: Option 1
Action: ('Option 1', 'Do something') # <<<<<< Return a Tuple?
The statement return a tuple which I've already unpacked it from main (). Here's a working example when option 'b' isn't selected.
M A I N M E N U
1. Settings
q. Quit
Select an option: 1
S U B M E N U
1. Perform an action
b. Back
q. Quit
Select an option: 1
Message: Option 1
Action: Do something
As stated, the issue is the recursive calling of the mainmenu in the submenu. In another word, the submenu created a new instance of the mainmenu each time the "back" key is pressed. To solve it, simply return to the mainmenu without calling another main menu.
From submenu:
elif option.lower () == 'b':
return
This method will return "None" to the mainmenu the while loop will need to account for that.
def mainmenu ():
d = None
msg = ''
while d is None:
....