0

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

patsy_794
  • 45
  • 6
  • `addToInventory` doesn't return anything, so it implicitly returns `None.` So `None` is passed to `displayInventory`, which causes your error. – Tom Karzes May 28 '19 at 21:09

3 Answers3

3

In your addToInventory function, you are not returning the modified dictionary but you are expecting it to be returned when you call it in your code at the end. Because nothing is returned in the function, it defaults to returning None so your displayInventory function is trying to loop over a NoneType variable. Because a dictionary is passed by reference in Python, you can modify the dictionary without having to return anything. Simply remove the assignment to backpack after you call addToInventory:

addToInventory(backpack, dragonLoot)

When you do that, we thus get the expected output:

Inventory:
45 gold coin
1 rope
1 dagger
1 ruby
Total number of items:48
rayryeng
  • 102,964
  • 22
  • 184
  • 193
1

Your function call to

backpack = addToInventory(backpack, dragonLoot)

is not returning anything, so your backpack variable is None by default, hence the NoneType error.

To fix this, remove the assignment to backpack when you call the function:

addToInventory(backpack, dragonLoot)
rayryeng
  • 102,964
  • 22
  • 184
  • 193
chefer
  • 401
  • 3
  • 5
0

What is happening is that you are reassigning the variable backpack to the output of addToInventory. However, addToInventory does not return any values, so backpack is being assigned to None by default. The NoneType error then occurs when you pass the backpack variable containing None to the displayInventory function.