Python 如果,elif&;!=,==布尔运算仅返回“0”;假;有投入
我正在创建一个interwes-ripoff(为了好玩!),while-True&if/elif/else语句只会返回false(而不是冒名顶替者)。我已经为这些名字创建了一个列表,列表中的两个随机元素将被选为冒名顶替者。但是,每当我输入的名称是冒名顶替者时,它只会返回 (玩家)不是骗子 这是我的密码Python 如果,elif&;!=,==布尔运算仅返回“0”;假;有投入,python,if-statement,variables,input,boolean,Python,If Statement,Variables,Input,Boolean,我正在创建一个interwes-ripoff(为了好玩!),while-True&if/elif/else语句只会返回false(而不是冒名顶替者)。我已经为这些名字创建了一个列表,列表中的两个随机元素将被选为冒名顶替者。但是,每当我输入的名称是冒名顶替者时,它只会返回 (玩家)不是骗子 这是我的密码 import sys, time, random names = ["player1", "player2", "player3", &
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.")
但是,每当我在输入中输入冒名顶替者时,它都会说它不是冒名顶替者?我认为这一行是问题的根源:
if talk!=冒名顶替者1或交谈!=冒名顶替者2:
假设impositor1
是player1,impositor2
是player2,根据PythonBoolean
expression操作符或if
语句的计算结果如下:
if player1!=impostor1
计算为False
,因为player1实际上等于impostor1
到目前为止还不错,但因为第一个测试是False
,Python只计算并返回右侧操作数,该操作数可以是True
或False
。在您的情况下,Python将对if talk!=impostor2
并返回True
,然后执行嵌套块。哦,如果您需要更干净的代码版本,我可以提供。我只需要知道这些if语句的错误,我不需要重写代码。请提供预期的结果。显示中间结果与预期结果的偏差。我们应该能够将单个代码块粘贴到文件中,运行它,并重现您的问题。你为一个三行问题发布了30行代码。