Interpretation of two-digit numbers

So I have problems with this code.

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" 

He pulls the score from the baseball game from MLB and sends it to reddit as follows:

4-3 Name of the winning team

However, I noticed that if one of the indicators has double digits, the code seems to only read the first digit, so a score of 10-2 would look like this:

2-10 Loss of team name

I searched a bit, and maybe I'm using the wrong search terms, but I can not find the answer here. Any help would be greatly appreciated.

+4
source share
1 answer

It looks like you are comparing strings:

 >>> "10" < "2" True 

Compare their whole version:

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

If the key is missing, then dict.get returns None by default. You can also pass your own default value.

 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 

Demo:

 >>> int("10") < int("2") False 
+4
source

Source: https://habr.com/ru/post/1488962/


All Articles