11. Making a Purchase: compute_bill(['apple']) returned 0 instead of 2 HELP?


#1

Hi, I've been stuck on this problem for a while and I can't seem to find solutions on other threads. Here is my code:

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!
food =["apple"]
def compute_bill(food):
    total = 0
    for item in food:
        prices[item] += total
    return total

And here is the exact error message:

Oops, try again. compute_bill(['apple']) returned 0 instead of 2

Some help would be greatly appreciated. Thanks!


#2

you might wanna change this line

prices[item] += total #same as saying prices[item]  = prices[item] + total

to this

total += prices[item] #same as saying total = total + prices[item]

You want to update the value of total not the value of the items hence that line you have in your code updates all the prices in the dictionary and sets them to 0 thats why you get 0


#3

That was a huge help, thanks!


#4

total += prices[item]