python - 如何使这个循环重复直到添加了正确的值,Python
问题描述
我试图让下面的代码重复,直到输入正确的字符串值之一。
while True:
if value in ['man united', 'man city', 'liverpool', 'chelsea']:
print(tabulate(data[value]))
break
if value not in ['man united', 'man city', 'liverpool', 'chelsea']:
print("You entered a wrong option, Please enter a correct option")
print(f"1: {options}")
我尝试了几种不同的方法,但无法完全实现我想要的。此代码在 python 函数中。
任何帮助是极大的赞赏。Python 新手
解决方案
这就是我认为你可以做到的方式, l 只是一个包含选项的列表,但你不必使用它。
l = ['man united', 'man city', 'liverpool', 'chelsea']
value = input()
while value not in l:
print("You entered a wrong option, Please enter a correct option")
print(f"1: {options}")
value = input() #or however you want to change the value variable
print(tabulate(data[value])) #this will execute only once the correct choice has been selcted
推荐阅读
- c# - 使用 Visual Studio 安装程序项目将 ASP .NET Core 服务安装到 IIS
- javascript - 使用反应样式组件渲染多个样式类
- python - awsebcli:通过自制软件安装了带有 python 3.9 版本的 EB CLI。项目中使用 Python 3.7 - 潜在问题?
- python - ffmpeg 无法以任何方式识别使用过的编解码器
- javascript - pm.response.json() 和 JSON.parse(responseBody) 之间的区别
- ios - 选择 TableView 单元格会激活多个部分中的行中的复选标记
- amazon-s3 - 如果我有一个 S3 生命周期规则来永久删除过期版本,那么当当前版本是删除标记时它是否也适用?
- javascript - nextjs 带空格的动态路由问题
- dart - Dart 架子 - 中间件和处理程序执行顺序
- javascript - 无法单击条纹结帐按钮