[英]Why doesn't my if statement not work in while loop?
So I have the code set up where if the input is not yes or no print("Type Yes or No") but even if I type yes or no the message is still printed.所以我设置了代码,如果输入不是 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":
This condition is always true.此条件始终为真。 A string cannot be equal to both "yes" and "no", so it is always unequal to at least one of them.
一个字符串不能同时等于“yes”和“no”,所以它总是至少不等于其中之一。
You mean你的意思是
if are_you_done != "yes" and are_you_done != "no":
or或者
if are_you_done not in {"yes", "no"}:
You have two conditions in your if statement and if either one is true, then the question will be printed. if 语句中有两个条件,如果其中一个为真,则将打印问题。 In other words, for the question to not be printed, then both must be true.
换句话说,对于不打印的问题,那么两者都必须为真。 In other words, for the question to be skipped, the value in
are_you_done
must be both yes
and no
at the same time.换句话说,对于要跳过的问题,
are_you_done
的值必须同时为yes
和no
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.