我开始自学python是为了好玩,我正在写这篇文章,但没有得到我想要的结果:
if no1234 == 0:
print "Player and Computer tie!\n"
elif no1234 == 1 or 2:
print "Player wins!\n"
elif no1234 == 3 or 4:
print "Computer wins!\n"
计算机获胜不会出现,是不是我忘记了什么
我开始自学python是为了好玩,我正在写这篇文章,但没有得到我想要的结果:
if no1234 == 0:
print "Player and Computer tie!\n"
elif no1234 == 1 or 2:
print "Player wins!\n"
elif no1234 == 3 or 4:
print "Computer wins!\n"
计算机获胜不会出现,是不是我忘记了什么
elif no1234 == 1 or 2:
被解析为elif (no1234 == 1) or (2):
这将永远是True
因为bool(2) == True
你正在使用一个or
语句。
你可能想要:
elif no1234 == 1 or no1234 == 2:
甚至:
elif no1234 in (1, 2):
这对您的其他elif
.
总而言之:
if no1234 == 0:
print "Player and Computer tie!\n"
elif no1234 in (1, 2):
print "Player wins!\n"
elif no1234 in (3, 4):
print "Computer wins!\n"
你需要这个:
if no1234 == 0:
print "Player and Computer tie!\n"
elif no1234 == 1 or no1234 == 2:
print "Player wins!\n"
elif no1234 == 3 or no1234 == 4:
print "Computer wins!\n"