if, elif & !=, == boolean operations only return "false" with inputs

if, elif & !=, == boolean operations only return "false" with inputs

我正在创建一个 Among Us ripoff(为了好玩!)而 while True & if/elif/else 语句只会 return false (不是冒名顶替者)与输入。我已经为名字创建了一个列表,列表中的 2 个随机元素将被选为 An Impostor。但是,每当我输入一个名字 The Impostor 时,它只会 return

(player) was not An Impostor.

这是我的代码;

import sys, time, random
names = ["player1", "player2", "player3", "player4", "player5", "player6", "player7", "player8", "player9", "player10"]
print("Players: ")
for x in names:
  print(x)
print('—————————————————————————')
impostor1 = random.choice(names)
impostor2 = random.choice(names)
crewmates = 8
impostors = 2
tries = 6
while True:
  talk = input("Guess who The Impostor(s) are. " + str(crewmates) + " Crewmates are left. " + str(impostors) + " Impostors are left. You have " + str(tries) + " tries left.")
  if talk in names:
    print(talk + " was voted for.")
    time.sleep(0.1)
    if talk != impostor1 or talk != impostor2:
      notimp = talk + " was not An Impostor. "
      names.remove(talk)
      for y in notimp:
        sys.stdout.write(y)
        sys.stdout.flush()
        time.sleep(0.05)
      crewmates -= 1
      tries -= 1
    elif talk == impostor1 or talk == impostor2:
      wasimp = talk + " was An Impostor. "
      names.remove(talk)
      for v in wasimp:
        sys.stdout.write(v)
        sys.stdout.flush()
        time.sleep(0.1)
      impostors -= 1
  else:
    print("That player was either ejected or is not a valid player.")

但是,每当我将 Impostor 放入输入中时,它都说它不是 An Impostor?

我认为这一行是问题的根源:

if talk != impostor1 or talk != impostor2:

假设 impostor1 是 player1,impostor2 是 player2,player1 中的某人 input,根据 Python Boolean 表达式运算符 or if 语句的计算结果如下:

if player1 != impostor1 评估为 False 因为 player1 确实等于 impostor1.

到目前为止一切顺利,但因为第一个测试是 False,Python 只是计算并 returns 右侧操作数,它可能是 TrueFalse。在您的情况下,Python 将评估 if talk != impostor2 和 return True,然后执行嵌套块。