In: Computer Science
PYTHON Exercise 4. Fantasy Game Inventory Imagine you have this inventory in a fantasy game: Item Number of this items Rope 1 Torch 6 Gold coin 42 Dagger 1 Arrow 12 1. Save the inventory in a data structure. 2. Write a function that will take any possible inventory and display it in a pretty format. 3. Write a function to add new items to the data structure. Make sure new group of items can be created. 4. Write a function to delete items (not a whole group) from the data structure.
# 1. Save the inventory in a data structure
inventory = {"Rope": 1, "Torch": 6, "Gold": 42, "Dagger": 1, "Arrow": 12}
# 2. Write a function that will take any possible inventory and display it in a pretty format.
def printInventory(d):
print("Item\t\tCount")
print("-----------------")
for x in d.keys():
print(str(x)+"\t\t"+str(d[x]))
print("\n")
# 3. Write a function to add new items to the data structure. Make sure new group of items can be created.
def addInventory(d, item, count):
d[item] = count
# 4. Write a function to delete items (not a whole group) from the data structure.
def deleteInventory(d, item):
del d[item]
# Testing methods
printInventory(inventory)
addInventory(inventory,"Silver",23)
printInventory(inventory)
deleteInventory(inventory,"Gold")
printInventory(inventory)


