1

Here is my code:

import math
import json

def make_teams_games():

cows=[0 for x in range (3)]
counter = 0
with open('2016ultimatedata.json') as jsonfile:
    d2016 = json.load(jsonfile)                                                     

    for i in d2016['teams']:
        if i['name'] == 'Oberlin (Flying Horsecows)':
            cows[0] = i['name']
            cows[1] = i['id']
            cows[2] = i['usau_rating']['score']
    for i in d2016['teams']:
        if i['usau_rating']['score'] != None:
            if i['usau_rating']['score'] > (cows[2]-120) and i['usau_rating']['score'] < (cows[2]+120):
                counter = counter+1
            print(counter)
    teams = [[0 for x in range (3)] for y in range (counter)]

make_teams_games()

Im aware that the third if statement can probably be written using a absolute value, but the error message is being thrown the line above. Why am I getting this error?

Here is a sample of part of the json file:

    "teams": [
{
  "id": "dHP4OF2933HXVGrIpWK%2f4FCcI6ithl6x98Nwnx3%2b6Ic",
  "usau_rating": {
    "wins": 10,
    "score": 1330.0,
    "losses": 8
  },
  "name": "Alabama-Birmingham (Inferno)"
},
{
  "id": "Bh3IMJNlD144zClqw0PgIIksuF%2fKC5MCDoo%2fbjsZ0f0",
  "usau_rating": null,
  "name": "Wisconsin-Stout (Yeti Ultimate)"
},
{
  "id": "QwWYWgLFpfsquPuzXaPZ1jc1hlf3kw%2bUWQEqdH5FGbc",
  "usau_rating": {
    "wins": 14,
    "score": 1107.0,
    "losses": 4
  },
  "name": "RIT (RIT Spudheds)"
},
{
  "id": "%2bIG3fKP7FAnAoAmq5paF760u9emIIlzIWoGNKGzb1zs",
  "usau_rating": {
    "wins": 9,
    "score": 846.0,
    "losses": 11
  },
  "name": "Princeton (Clockwork)"
},
{
  "id": "rHK4D3huWbOjSr20VH%2f37Tq%2bcDh52EYpYqXPEHyBloQ",
  "usau_rating": {
    "wins": 11,
    "score": 1188.0,
    "losses": 7
  },
  "name": "Dayton (UD Ultimate)"
}

And here is the traceback:

    Traceback (most recent call last):
    File "game_diff_by_difference_in_team_diff.py", line 24, in <module>
    make_teams_games()
    File "game_diff_by_difference_in_team_diff.py", line 18, in    make_teams_games
    if i['usau_rating']['score'] != None:
    TypeError: 'NoneType' object is not subscriptable
tharvey
  • 57
  • 1
  • 10
  • always show full error message (Traceback) in question. There is information which line makes problem – furas Dec 06 '16 at 01:27

2 Answers2

2

Looks like i['usau_rating'] is None. Try

if i['usau_rating'] is not None and i['usau_rating']['score'] is not None:
    ....

This way you check if the first dictionary lookup is None before trying to subscript it.


sidenote: I used the is not semantic instead of != because is checks for type not just 'falsy' values like != does. You can check out this answer if you care to learn more

Community
  • 1
  • 1
Justin Bell
  • 800
  • 7
  • 20
0

usau_rating is 'null' (None in Python) for Wisconsin-Stout.

Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47