I'm a beginner in coding and I've got a function here that is supposed to add a key to a dictionary when it is called. It looks like this:
def check_inventory(key):
if items["inventory"] >= 1 and items[key] not in items:
items[key] = 1
items["inventory"] -= items["inventory"]
elif items["inventory"] >= 1 and items[key] in items:
items[key] += 1
items["inventory"] -= items["inventory"]
elif items["inventory"] < 1:
return False
print "inventory full"
Basically, if the inventory key is bigger or equal to one, and the key is not already in the items dictionary, I want the function to decrease the inventory and add the item. If inventory is bigger than one, I want it to add one to the key value. Finally, if inventory is smaller than 1, I want it to print "inventory full"
Here is the items dictionnary:
items = {"inventory": 1}
I call the function like this:
check_inventory("batteries")
this is the error it gives me:
File "C:\Users\ludov\Desktop\PythonFiles\learnpythonhardway\pers3.py", line 108, in <module>
check_inventory("batteries")
File "C:\Users\ludov\Desktop\PythonFiles\learnpythonhardway\pers3.py", line 28, in check_inventory
if items["inventory"] >= 1 and items[key] not in items:
KeyError: 'batteries'
Anyway hope you all can help me. Also if you can explain the error code, it would be great! Thanks!