12. Stocking Out, error unhashable type 'list'


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096#

I get this error message when I try to run my code

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: unhashable type: 'list'


shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!
def compute_bill(food):
    total = 0
    if stock[food] > 0:
        for food in food:
            total += prices[food]
            stock[food] -= 1
        return total.


#2

this line:

if stock[food] > 0:

food is a list, not a string. I would do the for loop first, and then the if statement (loop over the items in the list, and then see if they are still in the stock)


#3

Ok I tried that and now I get this error code: Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7

Here's the new function for reference:

def compute_bill(food):
    total = 0
    for food in food:
        if stock[food] > 0:
            total += prices[food]
            stock[food] -= 1
        return total

#4

a function ends the moment a return keyword is reached, which in your cases happens in the first run of the for loop. I would put the return at the same indent level as the for loop, so the whole loop can run.

You did take a look at your new error before posting again on the forum, right?


#5

its really took me a day at supermarket. the compute_bill function is called by calling shopping_list array. so we need to add foods[0:] as below line, so that the for loop will iterate all the shopping_list array.

for food in foods[0:]:


#6