我想知道是否有人能帮我这个忙。这是一个简单的橄榄球冠军播音员。代码有时运行得很好,而其他时候却不太好。
team1 = input ("Team 1: ")
score1 = input("Score: ")
team2 = input("Team 2: ")
score2 = input("Score: ")
if score1 >= score2:
print (team1 + " beat " + team2 + " " + score1 + "-" + score2)
else:
print(team2 + " beat " + team1 + " " + score2 + "-" + score1)这是一个正在工作的例子:
Team 1: england
Score: 35
Team 2: fiji
Score: 11
england beat fiji 35-11但是,当我输入它时,它不起作用:
Team 1: Wales
Score: 54
Team 2: Urguary
Score: 9
Urguary beat Wales 9-54有人能看出是怎么回事吗?任何帮助都很感激。
发布于 2015-11-02 04:44:53
您的比较没有按预期的方式工作,因为它是比较字符串,而不是数字。字符串按字典顺序进行比较,因此9比54高,就像Z按字母顺序排列在AA之后一样。
要使代码工作,请将用户获得的分数转换为使用int的整数。
team1 = input ("Team 1: ")
score1 = int(input("Score: "))
team2 = input("Team 2: ")
score2 = int(input("Score: "))发布于 2015-11-02 04:47:38
team1 = input ("Team 1: ")
score1 = input("Score: ")
team2 = input("Team 2: ")
score2 = input("Score: ")
if int(score1) >= int(score2):
print (team1 + " beat " + team2 + " " + score1 + "-" + score2)
else:
print(team2 + " beat " + team1 + " " + score2 + "-" + score1)发布于 2015-11-04 13:13:20
我建议比较值,输入值时使用整数或浮点数,或者在比较之前使其为整数或浮点数。
要么立即做
score1 = int(input("Score: ")
或紧接其后
score1 = input("Score: ")score1 = int(score1)
或者就像比较
if int(score1) >= int(score2):
https://stackoverflow.com/questions/33470871
复制相似问题