Unhashable type error


#1



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


When I try to run my code, it gives me the error "compute_bill(['apple']) resulted in a TypeError: unhashable type: 'list'". I've pulled this into IDLE just to see if it was an issue with the in-browser compiler, but still no luck


This is for the Making a Purchase section, and in theory should check if the food item is in prices, and then add the price to total.


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

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}
    
def compute_bill(food):
    total = 0
    if food in prices:
        total = total + prices[food]
        return total


#2

Hi @gusbyrd,

You need a for loop here instead of an if block ...

    if food in prices:

Also, you should not reuse the name of the parameter, food, as the name of another variable. Your for loop header should be something like this ...

    for item in food:

A few other changes are necessary in order to use the variable item within the loop.

Make sure the return statement is not part of the loop. To do so, remove one level of indentation from it.


#3

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