learningpython/inventory.py

23 lines
693 B
Python
Executable file

#! /usr/bin/env python3
def displayInventory(inventory):
print("Inventory:")
output = ""
item_total = 0
for k,v in inventory.items():
output += str(v)+" "+k+"\n"
item_total+= v
output += "Total number of items: "+str(item_total)
return output
def addToInventory(inventory, addedItems): #modifies in place!
for i in addedItems:
inventory.setdefault(i, 0)
inventory[i] += 1
inventory = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
print(displayInventory(inventory))
addToInventory(inventory, dragonLoot)
print(displayInventory(inventory))