12. Stocking Out resulted in 0


This is the Error I am receiving:
Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7

I have checked the forums and I can't figure out what I am doing wrong.

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 item in food:
        if stock[item] > 0:
            total += prices[item]
            stock[item] -= 1
        return total


a function ends the moment a return keyword is reached, in your case, a return keyword is reached in the first run of the loop, causing the function (thus the loop) to end

try changing the indent of return, so the whole for loop can run


try stock[item] = stock[item] -1 instead
because we need to subtract 1


-= is a shorthand for doing exactly that


i know but it didn't work for me


Thanks. It was the indenting, which is embarrassing as I read that in basically every thread about this. But it finally clicked.
And yes, I do believe I also had to change the subtraction for the lesson to complete.