I am coding a game inventory and i have a error message in my add to inventory function. what i am trying to do here is create a new key if the item is not already in the dict and give it the value 1 and if the item is already in the inventory it increases its value by 1. Happy for any Help i get:D
def add_to_inventory(inventory, added_items):
for row in inventory:
if added_items[row] not in inventory:
inventory[added_items[row]] = 1
if added_items[row] in inventory:
inventory[added_items[row]] =1
This is the error i get:
TypeError: tuple indices must be integers or slices, not str
CodePudding user response:
You are checking the index of an item with a string.
my_items = ('a', 'b', 'c')
my_items['a']
TypeError: tuple indices must be integers or slices, not str
Loop over added_items instead:
for item in added_items:
if item in inventory:
inventory[item] = 1
else:
inventory[item] = 1
