-2

我开始自学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"

计算机获胜不会出现,是不是我忘记了什么

4

2 回答 2

5

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"
于 2013-10-20T06:07:24.870 回答
3

你需要这个:

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"
于 2013-10-20T06:07:57.813 回答