巨蟒-如何创建一个循环来提示一个特定的答案



今天是我编码的第二天,有人能帮我吗?

我正在编写一个代码,提示用户回答是、是、y、y或否、否、n、n。如果用户用另一个答案回答,系统应该提示用户再次回答。下面是我写的当前代码。

然而,我做不到,有人能帮忙吗?我应该使用哪种循环吗?

print("Please answer the question below in y or n!!")
answer1 = input("Do you want apple?")
answer_yes = "y"
answer_no = "n"

if answer1 == answer_yes:
print("Here you go!")
elif answer1 == answer_no:
print("There are apples for you in the fridge!")
else:
print(input("Do you want apple?"))

这里有一个建议:

print("Please answer the question below in y or n!!")
answer = ''
answer_yes = ['yes', 'YES', 'y', 'Y']
answer_no = ['no', 'NO', 'n', 'N']
while answer not in answer_yes and answer not in answer_no:
answer = input("Do you want apple? ")
if answer in answer_yes:
print("Here you go!")
elif answer in answer_no:
print("There are apples for you in the fridge!")

输出:

Please answer the question below in y or n!!
Do you want apple? n
There are apples for you in the fridge!

尝试一个while True循环,当它们回答正确时再尝试break

相关内容

最新更新