解释两位数字

时间:2013-06-30 15:06:10

标签: python digits

所以,我在使用这段代码时遇到了一些麻烦。

if s.get("home") < s.get("away"):
        scoringplays = scoringplays + s.get("away") + "-" + s.get("home") + " " + game.get("away_team_name")
    elif s.get("home") > s.get("away"):
        scoringplays = scoringplays + s.get("home") + "-" + s.get("away") + " " + game.get("home_team_name")
    else:
        scoringplays = scoringplays + s.get("home") + "-" + s.get("away") + " Tied"

它从MLB中拉出棒球比赛的得分并将其发布到reddit,如下所示:

4-3获胜队名

但是,我注意到如果其中一个分数是两位数,代码似乎只读取第一个数字,所以得分10-2会显示如下:

2-10输掉球队名称

我搜索了一下,也许我使用了错误的搜索词,但我似乎无法在这里找到答案。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

看起来你在比较字符串:

>>> "10" < "2"
True

比较整数版本:

if int(s.get("home")) < int(s.get("away"))

如果dict中缺少密钥,则dict.get默认返回None。您也可以传递自己的默认值。

home_score = int(s.get("home", 0))  # or choose some other default value
away_score = int(s.get("away", 0))

if home_score < away_score:
     #do something

演示:

>>> int("10") < int("2")
False
相关问题