Stocking Out help!


#1

Hi, I have been stuck on this for ages and I can't work it out. I get the error ' calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7'

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!

def compute_bill(food):
    total = 0
    for key in food:
        if stock[key] > 0:
            total += prices[key]
            stock[key] -= 1
        else:
            return total

thanks in advanced :smile:


#2

Hi @mavronator ,

This else block only returns a value when stock[key] > 0 ...

else:
    return total

So that total is always returned after the loop completes all its iterations, remove the else: header, and only indent the return statement by the same degree as the for loop header, namely by one level.