python-3.x - 如果输入的值错误,我将如何返回菜单?
问题描述
所以我有这个代码
import csv
def menu():
print ("""
1.Enter airport details
2.Enter flight details
3.Enter price plan and calculate profit
4. Clear data
5.Quit
""")
menu()
b = input("What would you like to choose?")
if b == '4':
print('Goodbye')
if b == '1':
a = input('Enter three letter code: ')
if a == 'LPL' or a == 'BOH':
d = input('Enter overseas airport code: ')
airport_info = open('airports.csv', 'r')
csvreader = csv.DictReader(airport_info,delimiter = ',', fieldnames = ('overseasCode','overseasName','distLPL','distBOH'))
for row in csvreader:
if row['overseasCode'] == d:
print(row['overseasName'],d)
因此,如果用户输入的内容与 LPL 或 BOH 不同,则应显示错误代码,并且用户返回菜单重试。我认为它必须是一个while循环,但我不确定如何实现它,所以有人可以帮忙吗?
解决方案
这确实是一个 while 循环。就像这样:
flag = False
while flag == False:
menu()
b = input("What would you like to choose?")
if b == '4':
print('Goodbye')
if b == '1':
a = input('Enter three letter code: ')
if a == 'LPL' or a == 'BOH':
flag = True
...
else:
print("Error")
推荐阅读
- django - 详细视图中的相关先前帖子
- javascript - How to parse JSON array from a GET request in react-native?
- javascript - 我需要使用 JS 为促销代码创建验证
- node.js - 导入 webpack 导出的 mongoose 模式
- python - 在 python 日志中添加上下文变量
- c - 运行时错误 - 变量“arr”周围的堆栈已损坏
- asp.net - 如何在同一个 appsettings.json 中引用另一个键
- javascript - 从现有代码中创建多个函数
- laravel - 为什么我通过 bash 将 npm 安装到 VS 代码后无法运行“npm run dev”?
- javascript - 只执行第一行函数