2016-12-06 78 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中無)。

相關問題