python-3.x - 如何使用“while”语句不断地向用户提出相同的问题?
问题描述
我想一遍又一遍地问我的用户一个是或否的问题,并不断将“正确”或“不正确”的回答反馈给用户。
print("Answer Yes to this Question")
check = str(input())
while check == ['yes', 'y', 'Yes', 'Y']:
print('Correct!')
break
else:
print('Incorrect')
check = str(input('Try Again'))
我不希望代码退出或完成,用户应该能够不断回答问题。目前,当我运行代码时,无论我做什么,我在终端中得到的响应都是“不正确的”,之后我再输入一个输入,然后代码退出。我做错什么了?
解决方案
试试这个
while True:
print("Answer Yes to this Question")
check = str(input())
if check.lower() in ['yes', 'y']:
print('Correct!')
else:
print("Incorrect, try again")
推荐阅读
- node.js - NPM 无法在 Windows 10 上更新 Node
- python - 我想从 python 中的 JSON 文件中获取一个随机对象
- c# - XAML WPF 自定义控件库未在外部项目中显示嵌入图像
- java - 如何从firebase中的所有用户中检索特定的子数据
- javascript - Loadash对多列进行分组和求和
- php - 从选定的选项中获取 php 值
- python-3.x - 反向字符串递归函数循环
- javascript - 在 node.js 后端使用 gmail-send 时出现连接超时错误
- reactjs - 无法将调度连接到道具
- php - 从包含调用中包含的 PHP 传递变量