首页 > 解决方案 > 如何使用“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'))

我不希望代码退出或完成,用户应该能够不断回答问题。目前,当我运行代码时,无论我做什么,我在终端中得到的响应都是“不正确的”,之后我再输入一个输入,然后代码退出。我做错什么了?

标签: python-3.xwhile-loop

解决方案


试试这个

while True:
  print("Answer Yes to this Question")
  check = str(input())
  if check.lower() in ['yes', 'y']:
    print('Correct!')
  else:
    print("Incorrect, try again")

推荐阅读