Why is this code incorrect? Issue with dictionary function


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

I also got this error, but I dont understand how to resolve it.
" Oops, try again. compute_bill(['apple']) resulted in a TypeError: unhashable type: 'list' "

Any help and support would be appreciated.


I dont see a 'food' list in the code you put so the 'for f in food' is looking for the food list but cant find it.
try changing it to shopping_list

Also if this doesnt work, if you give me which number exersize this is on i can give more help since i will know which code of mine i have to look at and see where it went wrong


I am having the same problem with this!! HELP!


I'm not entirely sure what's wrong with your code but this is my code.

def compute_bill(food):
{indent} total = 0
{indent} for key in food:
{indent} {indent} total += prices[key]
{indent} return total

The "{indent}" are to show the number of indentations i used for it. Hope it helps.


your code should be total += prices[f] not

total += prices[food]