Stuck on Making a Purchase


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096


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


#2

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


#3

no, it should not, do you understand why?


#4

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


#5

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


#6

Oh! I see. Thank you


#7