Python KeyError: 'TOT' - What is it? Why am I getting it?

Python KeyError: 'TOT' - What is it? Why am I getting it?

我搜索了 Internet,但甚至找不到提及此特定键错误所代表的内容。此代码

Player_p_dict = {}      
for player in Players:
    pp = float(Player_dict[player][0])/Team_dict[Player_dict[player][1]]
    Player_p_dict[player] = pp

print Player_p_dict

returns错误信息

  Traceback (most recent call last):   File "FantasyNHL.py", line 818,
in <module>
     pp = float(Player_dict[player][0])/Team_dict[Player_dict[player][1]]
 KeyError: 'TOT'

其中 Player_dict 是一个包含列表条目的字典,而 Team_dict 是另一个字典(我知道这很奇怪)。

KeyError 表示您引用的字典键不存在。错误出在这两个调用之一中:

Player_dict[player]
# or
Team_dict[Player_dict[player][1]]

KeyError 发生,例如如果您尝试访问字典中不存在的键,

KeyError: 'TOT' 表示它要查找的密钥是 'TOT'

我看到你分配了 Player_p_dict[player] 但你正在阅读 Player_dict 上的密钥 player 可能是它