Skip to content

Instantly share code, notes, and snippets.

@cherylli
Created August 28, 2019 12:22
Show Gist options
  • Save cherylli/bb3b506cf77e74332ae6e0a1442d78af to your computer and use it in GitHub Desktop.
Save cherylli/bb3b506cf77e74332ae6e0a1442d78af to your computer and use it in GitHub Desktop.
Automate the Boring Stuff with Python Chapter 5 Practice Projects - Fantasy Game Inventory
def displayInventory(inv):
print("Inventory: ")
total = 0
for item, quantity in inv.items():
print(f'{quantity} {item}')
total += quantity
print(f'Total number of items: {total}')
def addToInventory(inventory, addedItems):
inv_copy = inventory.copy()
for item in addedItems:
inv_copy[item] = inv_copy.get(item, 0)+1
return inv_copy
inventory = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
displayInventory(inventory)
inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
inv = addToInventory(inv, dragonLoot)
displayInventory(inv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment