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