创建菜单和子菜单显示
Creating Menu and Submenu Display
我有以下主菜单代码:
>>> 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
>>>
子菜单代码:
>>> 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
>>>
每当我从子菜单发出 "back" 命令返回主菜单时,整个菜单系统就会崩溃。看起来父菜单在它停止的地方继续调用另一个子菜单实例 where
p = ''
导致 value 为空。
下面是上述错误的示例:
>>> 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
>>>
但是,如果我从子菜单 select "option 1" 并且不使用 "back" 选项,一切都会按预期进行。
>>> 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'
>>>
如何修复 "back" 选项功能?
我试图在主菜单的选项 1 和我能想到的每个地方插入一个 return 语句,但它没有用。我缺乏一些逻辑,非常感谢任何解释或帮助。
...
elif option == '1':
d = submenu ()
return
...
更新 1
- 添加变量 "msg" 到选项 == '1' 下的主菜单和 return 元组。
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
- 解包并打印 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?
声明 return 一个元组,我已经从 main () 中解压了它。这是选项 'b' 未 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
发生这种情况是因为您 returning None
。
elif option.lower () == 'b':
mainmenu ()
return
您的代码正在递归调用 mainmenu()
,因此当您按 1 时,所有 return 值都会爬回到 "top," 并且在该过程中您引入了 NoneType
.
只需return调用该函数即可。
elif option.lower () == 'b':
return mainmenu ()
如前所述,问题是在子菜单中递归调用主菜单。换句话说,每次按下 "back" 键时,子菜单都会创建一个主菜单的新实例。要解决它,只需 return 到主菜单而不调用另一个主菜单。
来自子菜单:
elif option.lower () == 'b':
return
此方法将 return "None" 到主菜单 while 循环将需要考虑到这一点。
def mainmenu ():
d = None
msg = ''
while d is None:
....
我有以下主菜单代码:
>>> 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
>>>
子菜单代码:
>>> 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
>>>
每当我从子菜单发出 "back" 命令返回主菜单时,整个菜单系统就会崩溃。看起来父菜单在它停止的地方继续调用另一个子菜单实例 where
p = ''
导致 value 为空。
下面是上述错误的示例:
>>> 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
>>>
但是,如果我从子菜单 select "option 1" 并且不使用 "back" 选项,一切都会按预期进行。
>>> 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'
>>>
如何修复 "back" 选项功能?
我试图在主菜单的选项 1 和我能想到的每个地方插入一个 return 语句,但它没有用。我缺乏一些逻辑,非常感谢任何解释或帮助。
...
elif option == '1':
d = submenu ()
return
...
更新 1
- 添加变量 "msg" 到选项 == '1' 下的主菜单和 return 元组。
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
- 解包并打印 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?
声明 return 一个元组,我已经从 main () 中解压了它。这是选项 'b' 未 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
发生这种情况是因为您 returning None
。
elif option.lower () == 'b':
mainmenu ()
return
您的代码正在递归调用 mainmenu()
,因此当您按 1 时,所有 return 值都会爬回到 "top," 并且在该过程中您引入了 NoneType
.
只需return调用该函数即可。
elif option.lower () == 'b':
return mainmenu ()
如前所述,问题是在子菜单中递归调用主菜单。换句话说,每次按下 "back" 键时,子菜单都会创建一个主菜单的新实例。要解决它,只需 return 到主菜单而不调用另一个主菜单。
来自子菜单:
elif option.lower () == 'b':
return
此方法将 return "None" 到主菜单 while 循环将需要考虑到这一点。
def mainmenu ():
d = None
msg = ''
while d is None:
....