我正在制作一个快速的 zork 游戏,但我在使用“or”运算符时遇到了这个问题。我以为这很简单,但我无法弄清楚为什么这不起作用。现在,如果您输入“n”,您应该得到“这有效”,因为它等于字符串“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
使用后或打印出来。这可能是显而易见的-.-
表达式:
(response != 'n') or (response != 's')
对于任何字符串响应,将始终为 True。如果响应
为“n”
,则它不是“s”。
如果它是“s”
,那么它就不是“n”。
如果是其他任何东西,那么它不是“s”
,也不是“n”。
也许你打算使用和
那里?
如果响应
为 n
或 s
,则两个条件都将满足。最好的方法是
if response in ('n', 's'):
print ("it works")
else:
print ("this works")