PYTHON - 如何创建循环以提示特定答案

PYTHON - How to create a loop to prompt a specific answer

今天是我编码的第二天,有人可以帮助我吗?

我正在编写一个代码来提示用户回答 yes, YES, y, Y 或 no, NO, 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?"))

尝试 while True 循环,当他们回答正确时 break

这是一个建议:

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!