2015-04-05 85 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!"