Search code examples
pythonkeyerror

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


I scoured the internet, but couldn't even find a mention of what this particular keyerror represents. This code

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 the error message

  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'

Where Player_dict is a dictionary with list entries, and Team_dict is another dictionary(surprising, I know).


Solution

  • KeyError happens e.g. if you try to access a non-existing key in a dictionary,

    KeyError: 'TOT' means the key it was looking for is 'TOT'

    I see you assigned Player_p_dict[player] but you're reading the key player on Player_dict that could be it