"Making a Purchase" continually returning "compute_bill(['apple']) resulted in a TypeError: unhashable type: 'list.'"


#1


I've gone through this quite a bit right now to no avail. I'm not entirely sure what I'm doing wrong. This is the code that I currently have and if I'm reading the instructions correctly, it should be working:

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
    for x in food:
        total += prices[food]
    return total

The output printed back on the virtual terminal reads "none," and I keep getting the error message listed in the title of this post. In full, it reads:

"Oops, try again. compute_bill(['apple']) resulted in a TypeError: unhashable type: 'list.'"

I've been stuck on this and am really at a loss here. I've spent some time going through the forum and it looks like a lot of people have been stuck on this particular exercise. None of the other topics posted on this really had a solution which worked in my situation, though. Please help me out, it is greatly appreciated!


#2

looks like the error in this line

total += prices[food]

You have to add the price of each item in the food, in your case, the item in the food is x (not food).
It seems that 'food' should be replaced by 'x':
total += prices[x]
try this!


#3

It worked! Thanks so much! Could you tell me any more about this? I am very new to coding.


#4

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