Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 解读两位数_Python_Digits - Fatal编程技术网

Python 解读两位数

Python 解读两位数,python,digits,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

所以,这段代码有点麻烦

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"
如果s.get(“回家”)s.get(“离开”):
scoringplays=scoringplays+s.get(“home”)+“-”+s.get(“away”)+“+game.get(“home\u team\u name”)
其他:
scoringplays=scoringplays+s.get(“home”)+“-”+s.get(“away”)+“并列”
它从美国职棒大联盟(MLB)获取棒球比赛的分数,并将其发布到reddit,如下所示:

4-3获胜队名

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

2-10输队名


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

看起来您正在比较字符串:

>>> "10" < "2"
True
演示:

>>>int(“10”)
我在尝试该操作时遇到以下错误:TypeError:int()参数必须是字符串或数字,而不是“NoneType”@user2536657这意味着
s.get(“home”)
s.get(“away”)
返回
None
。您也可以使用默认值:
s.get(“home”,0)
,如果找不到键“home”,将返回0。抱歉。是python。谢谢你这么热情。
if int(s.get("home")) < int(s.get("away"))
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