Python infinite loop caused by or compound conditional but not when using not()
1 2 3 4 5 6 7 8 9 10 11  |     test = '' # This loop infinitely while test != 'O' or test != 'X': test = raw_input("Enter:").upper() # This works fine while not(test == 'O' or test == 'X'): test = raw_input("Enter:").upper()  | 
放置
问题在于你的结合:你未能正确应用DeMorgan的法律。 在分配否定时,您需要从
1  | not(test == 'O' or test == 'X')  | 
相当于
1  | test!= 'O' and test!= 'X'  | 
查看
我们不是在Python中做量子变量......至少还没有。 您必须编写一个带有自定义相等运算符的新类来使其工作。
...如果你一直去Pythonic,你会写
1  | while not test in ('O', 'X'):  | 
或 - 甚至更简单
1  | while not test in 'OX':  |