gpt4 book ai didi

python - 为什么我的 if 语句在 while 循环中不起作用?

转载 作者:行者123 更新时间:2023-12-01 06:40:39 25 4
gpt4 key购买 nike

所以我设置了代码,如果输入不是 yes 或 no print("Type Yes or No"),但即使我输入 yes 或 no,消息仍然会打印。

mylist = []
are_you_done = "no"
while are_you_done != "yes":
grocery_item = input("Add to Grocery ")
are_you_done = input("Are you done? ").lower()
mylist.append(grocery_item)
if are_you_done != "yes" or are_you_done != "no":
print("Type Yes or No")
are_you_done = input("Are you done? ").lower()



for item in mylist:
print(item)

最佳答案

if are_you_done != "yes" or are_you_done != "no":

这个条件始终为真。一个字符串不能同时等于“yes”和“no”,因此它总是不等于其中至少一个。

你的意思是

if are_you_done != "yes" and are_you_done != "no":

if are_you_done not in {"yes", "no"}:

关于python - 为什么我的 if 语句在 while 循环中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59473189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com