宣布获胜者代码

时间:2015-11-02 04:40:09

标签: python-3.x

我想知道是否有人可以帮助我。这是一个简单的橄榄球冠军 播音员。代码似乎有时运行良好而不是其他时间。

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

任何人都可以看到有什么问题吗?任何帮助表示感谢。

3 个答案:

答案 0 :(得分:1)

你的比较并不像你期望的那样,因为它是比较字符串,而不是数字。字符串按字典顺序进行比较,因此9高于54,与Z按字母顺序排在AA之后的方式相同。

要使您的代码有效,请将您从用户获得的分数转换为int的整数:

team1 = input ("Team 1: ")
score1 = int(input("Score: "))
team2 = input("Team 2: ")
score2 = int(input("Score: "))

答案 1 :(得分:0)

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)

答案 2 :(得分:0)

我会建议 要比较该值,请在输入值时使用整数或浮点数,或在比较之前将其设为整数或浮点数。

要么立即执行

  

score1 = int(input("Score: ")

或紧接在

之后
  

score1 = input("Score: ")
  score1 = int(score1)

或者只是比较

  

if int(score1) >= int(score2):