2016-12-06 74 views
0

这里是我的代码:“NoneType”对象未标化的

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()

林意识到,第三if语句大概可以使用写入一个绝对值,但错误信息正在被抛出上面的行。 为什么我得到这个错误?

这里是JSON文件的部分样本:

"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)" 
} 

这里是回溯:

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 
+0

始终显示有问题的完整错误消息(Traceback)。有信息哪一行发生问题 – furas

回答

0

看起来像我['usau_rating']是None。尝试

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

这样,您可以在尝试为其下标之前检查第一个字典查找是否为None。


阿里纳斯:我用is not语义,而不是因为=检查is类型不只是“falsy”像!=值呢!如果您想了解更多信息,您可以查看this answer

+1

这不会做任何不同的事情,我的'如果我['usau_rating'] ['score']!=无:'工作或这 – tharvey

+0

对不起,我忘了我之前['usau_rating ']在我的第一篇文章。自己编辑并测试它 - 没有错误。 –

+0

谢谢哈哈,现在它工作 – tharvey

0

usau_rating是 '空' 的威斯康星 - 斯托特(Python中无)。

相关问题