2015-04-05 163 views
-1

我想爲Yahtzee的遊戲做一個記分員,但是每當我把變量寫入players時,它總是會打印"I think that's enough."請幫我理解這裏發生了什麼以及我如何修復它。請幫我理解這裏發生了什麼

#Yahtzee Scorekeeper 

if __name__ == "__main__": 

    players = raw_input("How many people will be playing?") 

    if players == 1: 
     print "You can't play Yahtzee by yourself!" 
    elif players == 2: 
     print "Ready to start with " + str(players) + " players!" 
    elif players == 3: 
     print "Ready to start with " + str(players) + " players!" 
    elif players == 4: 
     print "Ready to start with " + str(players) + " players!" 
    elif players == 5: 
     print "Ready to start with " + str(players) + " players!" 
    elif players == 6: 
     print "Ready to start with " + str(players) + " players!" 
    elif players == 7: 
     print "Ready to start with " + str(players) + " players!" 
    elif players == 8: 
     print "Ready to start with " + str(players) + " players!" 
    elif players > 8: 
     print "I think that's enough." 
+0

另請注意,您的代碼非常重複;情況2-7的代碼是相同的,所以可以簡單地進行簡化。 – jonrsharpe 2015-04-05 22:23:45

回答

1

raw_input產生一個字符串,並且將它與一個數字進行比較。在Python 2中,結果如您所見。請嘗試以下操作:

players = int(raw_input("How many people will be playing?")) 

if players < 2: 
    print "You can't play Yahtzee by yourself!" 
elif players > 8: 
    print "I think that's enough." 
else: 
    print "Ready to start with " + str(players) " players!" 
相關問題