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

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