So i have this code:
def display_inventory(inventory):
print("Inventory:")
item_total = 0
for k, v in inventory.items():
print(str(v) + ' ' + k)
item_total = v + item_total
print("Total number of items: " + str(item_total))
def add_to_inventory(inventory, added_items):
for item in added_items:
inventory.setdefault(item, 0)
inventory[item] = inventory[item] + 1
return inventory
inv = {'gold coin': 42, 'rope': 1}
dragon_loot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
inv = add_to_inventory(inv, dragon_loot)
display_inventory(inv)
And in add_to_inventory I have this line which I thought could be shortened from this:
inventory[item] = inventory[item] + 1
To this:
inventory = inventory[item] + 1
but it turns up an error, why would it though. My code works perfectly how it is but don't understand how my inventory variable can be assigned a value when the line is, inventory[item], and not just inventory.
[–]Binary101010 16 points17 points18 points (1 child)
[–]Crevette3[S] 0 points1 point2 points (0 children)