我在子菜单中嵌套了一些函数,但这些函数并没有按我希望的方式工作



我有一个预订菜单,每次有人输入新的预订时,它都会将预订列表和座位表写入文本文件。

我有一个菜单,可以选择显示座位表、购买座位、嵌套管理菜单或退出。admin命令打开一个新菜单,可以清除预订列表的内容,返回主菜单或退出;或者至少应该是这样。我的错误检查肯定有问题,或者我不能用这种方式构建它。有人能看出我哪里错了吗?我有一个大程序,可以发布更多,但我宁愿只发布我有问题的部分。其他函数是在主程序级别(与菜单函数相同的级别(定义的,嵌套函数是否也应该在该级别定义?谢谢

def menu():
print(
'Select chioce from menu:nnP to purchase an available bus seatnD to display current bus seat chartnEnter password (admin) for admin menunQ to quit')
while True:
try:
menu_choice = str(input(' n')).upper()
if menu_choice == str('D'):
display()
break
elif menu_choice == str('P'):
book()
break
elif menu_choice == str('ADMIN'):
def clear_booking(): # the nested functions are not working-
print('Clearing bookings')
with open("bookings.txt") as f: 
f.write("")
def previous():
menu()            
print('CB to clear bookingsnR to return to main menunQ to quit')
admin_choice = str(input(' n')).upper
if admin_choice == str('CB'):
clear_booking()
elif admin_choice == str('R'):
previous()
elif admin_choice == str('Q'):
exit()
else:
print('Invalid choice')
exit()  
elif menu_choice == str('Q'):
print('exiting program')
exit()
else:
print('Value not recognized.')
exit()
except ValueError:
print('Invalid entry')
return # how do i get rid of the none from this returning correctly?
print(menu())

您忘记了在upper((函数调用的admin_choice = str(input(' n')).upper处的((。

此外,也许您可以在menu((函数体的最后一行返回一个带文本的字符串,而不仅仅是return

还要试着在问题的介绍性文本中包含你的问题陈述,以明确你的代码有什么问题,以及你想要实现什么。

最新更新