Another_Mark = raw_input("would you like to enter another mark? (y/n)")
while Another_Mark.lower() != "n" or Another_Mark.lower() != "y":
Another_Mark = raw_input("Do you want to input another mark? Please put either 'y' or 'n' this time")
if Another_Mark == "y":
print "blah"
if Another_Mark == "n":
print "Blue"
这不是我使用的实际代码,除了前三行。无论如何,我的问题是为什么 while 循环即使在我输入值“y”或“n”时仍然重复,当它再次询问您是否要在第三行输入另一个标记时。我陷入了一个无限重复的循环。 当 Another_Mark 的值更改为“y”或“n”时不应重复
请您参考如下方法:
尝试:
while Another_Mark.lower() not in 'yn':
Another_Mark = raw_input("Do you want to input another mark? Please put either 'y' or 'n' this time")
not in 如果在给定的可迭代对象中找不到给定的对象,则运算符返回 true,否则返回 false。这就是您正在寻找的解决方案:)
由于基本的 bool 代数错误,这无法正常工作。正如 Lattyware 所写:
not (a or b) (what you describe) is not the same as not a or not b (what your code says)
>>> for a, b in itertools.product([True, False], repeat=2):
... print(a, b, not (a or b), not a or not b, sep="\t")
...
True True False False
True False False True
False True False True
False False True True
