So, I am making an inventory system for my game but like always, there is an error. This is my fault of course, but I can't figure a way around it. I need the code to add an item into the dict without replacing the current key. For example, a player catches a fish:
inventory = {} #This is the dict to hold all the items
for x in range(1,10):
inventory['slot{0}'.format(x)] = {'type':'Fish'}
break
But if you catch say two fish, it will always take up the first slot
###Output:
{'slot1':{'type':'Fish'}}
So then I tried making an if a statement about if a slot was full try the next one
for x in range(1,10):
if inventory['slot{0}'.format(x)] != {}:
x += 1
inventory['slot{0}'.format(x)] = {'type':'Fish'}
break
Here is the expected output for two fish caught:
###Output
{'slot1':{'type':'fish'},'slot2':{'type':'fish}}
But then I get the Key error that the nested dictionary doesn't exist. So, I need some help.