Python无限循环引起或复合条件但不使用not()时

0 python

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

推杆not和使用之间有什么区别!=

Pru*_*une 17

问题在于你的结合:你未能正确应用DeMorgan的法律.你需要从翻转orand当您将否定.

not(test == 'O' or test == 'X')
Run Code Online (Sandbox Code Playgroud)

相当于

test!= 'O' and test!= 'X'
Run Code Online (Sandbox Code Playgroud)

看看逻辑test!= 'O' or test!= 'X':无论你给出什么字符test,它都不等于两个测试字符中的至少一个.F0r O,第二个条款是True; 因为X,第一个是True; 对于任何其他角色,这两个条款都是True.为了突破这一点,你需要,这是一个字符X,并O在同一时间.

我们不是在Python中做量子变量......至少还没有.您必须编写一个带有自定义相等运算符的新类来使其工作.