12. Stocking Out Code Error


#1



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


After a helping hand to get me to understand the previous module, I'm now very confused about the next one, #12. Stocking Out

When I run the code below I see this error:
Traceback (most recent call last):
File "python", line 26, in
File "python", line 22, in compute_bill
TypeError: unhashable type: 'list'

but I don't understand why.


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


#2

PS: I realize the indent for the return was incorrect, but that doesn't resolve my error :slight_smile:


#3

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

prices[food] should be prices[fruit], and so on for the rest of them.

I'm also new at this so I'm not sure if my explanation is correct,
but I think you are trying to find the value from the key of the dictionary.
fruit is a single key that matches the string from the food list.


#4

I don't think so. In any case, I tried switching it and I still get the same error.


#5

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

need to switch out all the [food]


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.