我正在尝试在python中创建一个带编号的菜单,我遇到一条错误消息,指出“解析时出现意外的EOF”,我不确定该怎么办,有人可以帮忙吗?
menu = 0
menu = int(input("pick an option:\n"
"1 print all records in system:\n"
"2 print employee details:\n"
"3 print total salary:\n"
"4 print average salary:\n"
"5 add new employee to system:\n"
"6 staff positions:\n"
"7 salarys over £30,000:\n"
"8 exit:\n\n"
"Option selected: ")
答案 0 :(得分:2)
您最后忘了一个括号:
menu = 0
menu = int(input("""\
pick an option:
1 print all records in system:
2 print employee details:
3 print total salary:
4 print average salary:
5 add new employee to system:
6 staff positions:
7 salarys over £30,000:
8 exit:
Option selected: """)) # here
这将起作用。
答案 1 :(得分:2)
这是菜单渲染的另一个更清晰灵活的版本。在下面的版本中,如果用户输入非数字字符,则不会出现ValueError。您可以随意更改和扩展您不喜欢的任何内容。
# Use OrderedDict or a list, if you're compiling on Python 3.5 or older
menu_items = {
"1": "Print all records in system:",
"2": "Print employee details:",
"3": "Print total salary:",
"4": "Print average salary:",
"5": "Add new employee to system:",
"6": "Staff positions:",
"7": "Salarys over £30,000:",
"8": "Exit:"
}
option = None
print("Pick an option\n")
while True:
print('\n'.join(["{} {}".format(idx, menu_item)
for idx, menu_item in menu_items.items()]))
option = input("\nOption selected: ")
if option not in menu_items:
print("Invalid option. Try Again")
else:
break
print("Do something with option {}".format(option)) # now you can even cast option to int without any error