A Day At The Supermarket



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


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


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 ?


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


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


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


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