A Day At The Supermarket


#1

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

It's giving me this error : Oops, try again. compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5

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 fruit in food :
        total += prices[fruit]
        return total

#2

Last line of your code, return should be at the same indent level as your for loop. This way, the for loop runs, and the sum of total gets returned. Currently, the total is returned after 1 run of the for loop


#3

Alright i seee...
Thanks A Lot !
And Also , i'm working on a python program that would help me with my vectors math problems .
I'm having some issues , can i post it in Q&A And get some help ?


#4

You could post it in the corner bar, i wouldn't get your hopes up, those kind of questions are very time consuming to answer, but you can try it


#5

It would return total after each run of the loop, wouldn't it?


#6

No, the moment return is reached, the function ends. (including the loop inside the function), so the for loop will run only once, which is why you want the return outside the for loop


#7

Yes , exactly as stetim said , i'm not used to paying that much intention for indentation , but in python you always should