while True:
game = input("Would you like to play: Guess the integer(i) or true/false(t). **Case Senestive** ")
if not name.isalpha():
print (name, "Enter the correct values")
continue
if not game == "i" or game == "t" or game=="integer" or game=="true/false":
print ("Enter i, integer or true/false, t")
continue
else:
break
如果他们不输入'i'
或'integer'
,我会转到not game == "i" or game=="integer"
部分。每当我输入i
时,它都有效,但在我输入integer
时却无效。
答案 0 :(得分:2)
您需要在or
条件下加上括号:
if not (game == "i" or game == "t" or game=="integer" or game=="true/false"):
否则Python将其视为(not game == "i") or ...
。
最好在套装上使用not in
:
if game not in {"i", "t", "integer", "true/false"}:
这不仅更短,而且更快;只进行一次检查常数时间检查,而不是对每个字符串进行单独的==
测试。