我正在創(chuàng)建一個“我們之中”的盜版(為了好玩?。?,而 while True 和 if/elif/else 語句只會返回 false(不是冒名頂替者)的輸入。我創(chuàng)建了一個姓名列表,列表中的 2 個隨機元素將被選為An Impostor。但是,每當我輸入The Impostor的名稱時,它只會返回(玩家)不是冒名頂替者。這是我的代碼;import sys, time, randomnames = ["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 = 8impostors = 2tries = 6while 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.")但是,每當我將冒名頂替者放入輸入中時,它都會說它不是冒名頂替者?
1 回答

慕容森
TA貢獻1853條經(jīng)驗 獲得超18個贊
我認為這一行是問題的根源:
if talk != impostor1 or talk != impostor2:
假設(shè)impostor1
isplayer1 和impostor2
isplayer2 以及input
player1中的某個人,根據(jù)PythonBoolean
表達式運算符,or
該if
語句的計算結(jié)果如下:
if player1 != impostor1
評估為False
因為player1 確實等于impostor1
。
到目前為止一切順利,但由于第一個測試是False
,Python 只是計算并返回右側(cè)操作數(shù),該操作數(shù)可能是True
或False
。在你的情況下,Python 將評估if talk != impostor2
并返回True
,然后執(zhí)行嵌套塊。
添加回答
舉報
0/150
提交
取消