2013-10-20 353 views
-2

我已經開始對我自己的快樂學習Python和我寫這並沒有得到我想要的結果:的Python如果else語句

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" 

計算機勝會顯示不出來,是有什麼IM忘記

+1

Downvoted詢問一個常見問題。 –

回答

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" 
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"