I'm doing the practice project in chapter 5 of automate the boring stuff. It asks to define a function with two parameters (a dictionary and a list) and for the numerical dictionary values to be updated with each common occurrence between the dictionary key and list value.
I've tried defining a function to return a listed inventory based on value key pairs when a dictionary is passed as a parameter (first practice project). Then I created a second function to account for additional items to be added to the dictionary inventory, passed as a list.
backpack = {'gold coin':42, 'rope':1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
def addToInventory(inventory, addedItems):
for item in addedItems:
inventory.setdefault(item,0)
inventory[item] = inventory[item] + 1
def displayInventory(inv):
print('Inventory:')
itemTotal = 0
for k, v in inv.items():
print(v, k, sep= ' ')
itemTotal += v
print('Total number of items:' + str(itemTotal))
backpack = addToInventory(backpack, dragonLoot)
displayInventory(backpack)
I receive the following error:
for k, v in inv.items():
AttributeError: 'NoneType' object has no attribute 'items'
I've tried but cant see the fault, especially since the first project, returning the value-key inventory, worked OK and is essentially the same as the displayInventory() function.
I could have found a solution easily but want to see where I am going wrong specifically. Thanks