Advertisement
ywei05

Dictionary Inventory

Jan 8th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. def displayInventory(inventory):
  2.   print("Inventory:")
  3.   item_total = 0
  4.   for k, v in inventory.items():
  5.     print(str(v) + ' ' +  str(k))
  6.     item_total += v
  7.   print("Total number of items: " + str(item_total))
  8.  
  9. def addToInventory(inventory, addedItems):
  10.     for i in range(len(addedItems)):
  11.         inventory.setdefault(addedItems[i], 0)
  12.         inventory[addedItems[i]] = inventory[addedItems[i]] + 1
  13.     return inventory
  14.  
  15. inv = {'gold coin': 42, 'rope': 1}
  16. dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
  17. inv = addToInventory(inv, dragonLoot)
  18. displayInventory(inv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement