Stuck on Making a Purchase


i get error message Oops, try again. compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5

Replace this line with your 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 item in food:
        total += prices[item]
        return total


thanks, sorted. return should not be in the for loop.


no, it should not, do you understand why?


No, i don't. Can you please explain?


of course, that is why i asked. A function ends the moment a return keyword is reached (the function returns something, end of function) even while the function is running a loop

So in your case, that happens in the first run of the loop. If you change the indent of return like you did, the whole loop finishes running, and then returns the total


Oh! I see. Thank you