Python循环不断重复?为什么?

1 python while-loop

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"
Run Code Online (Sandbox Code Playgroud)

这不是我正在使用的实际代码,除了前三行.无论如何我的问题是为什么while循环仍然重复,即使我输入值'y'或'n',当它再次询问你是否想在第三行输入另一个标记时.我陷入了一个无限重复的循环中.当Another_Mark的值更改为"y"或"n"时,不应重复此操作

Pio*_*uga 5

尝试:

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")
Run Code Online (Sandbox Code Playgroud)

not in如果在给定的iterable中找不到给定的对象,则operator返回true,否则返回false.所以这是你正在寻找的解决方案:)


由于布尔代数错误,这无法正常工作.正如Lattyware所写:

不是(a或b)(你所描述的)不是和不是b(你的代码所说的)

>>> 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
Run Code Online (Sandbox Code Playgroud)