0

I have 2 dict. One with local player data and one listing the players with subdictionaries:

      class GameData:
        def __init__(self):
          self.player  =  {'id'           : 1453339642,
                           'positionX'    : 123,
                           'positionY'    : 0
                          }

          self.players =  {1453339642:
                                  {'name'         : "Admin"}
                          }
     gameData = GameData()

Then I print out just to check if everything works:

            for x in gameData.player:
                print (str(x),':',gameData.player[x])

            print("\n\n")

            for x in gameData.players:
                print (str(x))
                for y in gameData.players[x]:
                    print ('    ',y,':',gameData.players[x][y])
            print("\n\n")

This results in:

            id : 1453339642
            positionY : 0
            positionX : 123

            1453339642
                 name : Admin

When I now want to access the player's id in players for instance with

            #print(str(type(gameData.player)))
            #print(str(type(gameData.players)))
            print(str(type(gameData.players[1453339642])))

I get KEYERROR as a result. Why?

1 Answers1

0

If I put this in a file, it works:

class GameData:
    def __init__(self):
        self.player  =  {'id'           : 1453339642,
                         'positionX'    : 123,
                         'positionY'    : 0
        }

        self.players =  {1453339642:
                         {'name'         : "Admin"}
        }
gameData = GameData()
print(str(type(gameData.players[1453339642])))

Only indentation differs from your code. There must be something happening to gameData between instantiation and the final print.

Patrick Fournier
  • 600
  • 6
  • 19
  • I checked everything for type now: it says that 1453339642 in player is int but in players it is a string. How can I convert it in players to a str? – user5526679 Jan 21 '16 at 02:44
  • Yes absolutely this is my problem here because it creates my dict keys as strings. This works: print(str(type(gameData.players[str(1453339642)]))) – user5526679 Jan 21 '16 at 02:48
  • I am not sure to understand. Looking at your code, `gameData.player['id']` is an int (there is no quotes around the value). This is the same for `1453339642` in `gameData.players`: it is an int because there is no quotes around it. – Patrick Fournier Jan 21 '16 at 02:50
  • Yeah I summarized it from a bigger project. In this project I append to the dict with gameData.players.update({ID and then it creates this as string for some reason I still do not get. – user5526679 Jan 21 '16 at 02:53
  • I see. You can use `int(id)` to convert to int. Also, you can just `print(gameData.players)` and you will see if your keys are strings or numbers. – Patrick Fournier Jan 21 '16 at 02:55
  • Ah it is because it goes to json format and back. Json converts my int key to a string when converting. Yes how do I change ALL keys back to int please? http://stackoverflow.com/questions/9304528/why-json-allows-only-string-to-be-a-key – user5526679 Jan 21 '16 at 03:00
  • You have to iterate over your dictionnary and copy the values to a new int key, then delete (`del`) the string key from the dict. – Patrick Fournier Jan 21 '16 at 03:05