我正在开发一个python项目,它包含一个选项菜单,这是源代码:
def option_menu():
"""
Prints options list
"""
choices_list = [
'1: Sniffing',
'2: Information gathering',
'3: Social Engineering',
'4: Network attacks',
'99: Information',
'exit: Exit'
]
bunch = "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
sys.stdout.write(HEADER)
print(bunch.center(80, ' '))
sys.stdout.write(BOLD)
for item in choices_list:
print(item.center(80, ' '))
sys.stdout.write(HEADER)
print(bunch.center(80, ' '))
sys.stdout.write(ENDC)
choose = input("Choose: ")
if choose == 1:
main_information()
elif choose == 2:
main_information()
elif choose == 3:
main_information()
elif choose == 4:
main_information()
elif choose == 99:
main_information()
elif choose == exit:
terminal_stop()
else:
print("You must select a number from the list")
如您所见,我使用if和else语句使它起作用,当我将不在列表中的数字放入打印机时,它会打印我告诉它打印的内容。问题是,如果我放字母或其他不是数字的东西(不在列表中),则会出现此错误:
Choose: e
Traceback (most recent call last):
File "main.py", line 6, in <module>
option_menu()
File "/home/adrian/mountainproject_1.0/functions.py", line 54, in option_menu
choose = input("Choose: ")
File "<string>", line 1, in <module>
NameError: name 'e' is not defined
我该怎么办?,其行为应是这样的:如果用户输入与列表中的任何数字都不相同,则打印“您必须从列表中选择一个数字”。问题在于它不适用于字母