0

我正在制作一个快速的 zork 游戏,但我使用“或”运算符遇到了这个问题。我认为这很简单,但我不明白为什么这不起作用。现在如果你输入“n”,你应该得到“this works”,因为它等于字符串“n”。相反,它会打印出“它有效”和“这有效”,所以很明显我使用了“或”错误。

   x=0
    while x<20:
        response = input("HI")
        if response!= 'n':
            print("it works")

        if response == 'n':
            print("this works")
        x+=1

使用前或它有效

x=0
while x<20:
    response = input("HI")
    if (response!= 'n') or (response != 's'):
        print("it works")


    if (response == 'n') or (response == 's'):
        print("this works")
    x+=1

使用后或打印出来。这可能很明显-.-

4

2 回答 2

4

表达方式:

(response != 'n') or (response != 's')

对于任何字符串响应,将始终为 True 。如果response'n',则不是's'。如果是's',则不是'n'。如果它是其他任何东西,那么它不是's',它不是'n'

也许你打算在and那里使用?

于 2013-12-18T01:31:37.750 回答
2

如果responsens,则两个条件都将满足。最好的方法是

if response in ('n', 's'):
    print ("it works")
else:
    print ("this works")
于 2013-12-18T01:32:29.403 回答