I have a json file of players and I want to update their stats, but for some reason this raises an error
Here's my code:
players = _load('players.json') #this is a load function I use
players[user]['coins'] = 1
And here is the error:
File "main.py", line 56, in index
players[user]['coins'] = 1
KeyError: 'player1'
I've used this method of updating stats for a while now, doesn't work for some reason
CodePudding user response:
The key error is being thrown since the players dict does not have a key in it for the variable user.
Check that user exists in players prior to doing the call:
players = {}
user = 'player1'
if user not in players:
players[user] = {}
players[user]['coins'] = 1
