【发布时间】:2016-01-06 05:57:39
【问题描述】:
我的小程序使用 Riot API(游戏),我将玩家放入“盟友团队”或“敌人团队”。由于数据来自 JSON,因此涉及到很多列表和 dicts,我的问题可能源于那里,尽管我一直无法找到。
这是导致问题的部分:
first_game_test = game_list[0]
summ_team_ID = first_game_test["teamId"]
summoners_in_game = first_game_test["fellowPlayers"]
ally_team = []
enemy_team = []
for i in range(len(summoners_in_game)):
for name, value in summoners_in_game[i].iteritems():
if summoners_in_game[i]["teamId"] == summ_team_ID:
#if summoners_in_game[i] not in ally_team:
summoner_name = idtosummoner.idToSummonerName(summoners_in_game[i]['summonerId'])
summoner_champ = champion_id.champIdGet(summoners_in_game[i]['championId'])
ally_team.append({summoner_name: summoner_champ})
else:
#if summoners_in_game[i] not in enemy_team:
enemy_team.append(summoners_in_game[i])
idtosummoner 和 champion_id 模块已被多次检查;我很确定这个问题不是源于那里。
如您所见,我使用了一个简单的重复检查修复(已注释掉)。然而,它开始混淆进一步的编码:summoner_name 和 summoner_champ 变量在第 3 或第 4 个索引处导致错误(我还没有将这些行添加到 else,因为我想解决这个问题首先)。
控制台输出显示如下:
PS C:\Users\ptnfolder> python matchhistory.py
Nussen
Nussen
Nussen
kimbb
Traceback (most recent call last):
File "matchhistory.py", line 67, in <module>
matchHistory("thebirdistheword")
File "matchhistory.py", line 39, in matchHistory
print idtosummoner.idToSummonerName(summoners_in_game[i].get('summonerId'))
File "C:\Users\ptnfolder\idtosummoner.py", line 10, in idToSummonerName
champ_name_dict = json_data[str(summID)]
KeyError: '29716673'
奇怪的是 KeyError 实际上应该解析为 'kimbb' - 因为 for 循环以某种方式将每个条目增加三倍 -;它运行一次,然后程序崩溃。
【问题讨论】:
标签: python json api for-loop dictionary