/Fantasy-Game-Inventory

It's a practice project for python beginners.

Primary LanguagePython

From the book: Automate the Boring Stuff with Python. Writer: Al Sweigart

List to Dictionary Function for Fantasy Game Inventory

It's a practice project for python beginners.

👉 Imagine that a vanquished dragon’s loot is represented as a list of strings like this:

dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby'] 

👉 No.1 Write a function named addToInventory(inventory, addedItems), where the inventory parameter is a dictionary representing the player’s inventory (like in the previous project: Inventory ) and the addedItems parameter is a list like dragonLoot.

👉 The addToInventory() function should return a dictionary that represents the updated inventory. Note that the addedItems list can contain multiples of the same item.

👉 Your code could look something like this:

def addToInventory(inventory, addedItems):
       your code goes here
inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby'] 
inv = addToInventory(inv, dragonLoot)
displayInventory(inv) 

The previous program (with your displayInventory() function from the previous project: Inventory ) would output the following:

Inventory:
45 gold coin
1 rope
1 ruby
1 dagger
Total number of items: 48