def compute_bill(food):
    total = 0
    for food in shopping_list:
    return total

Change for food in shopping_list: to be for item in food:
now any list put into compute_bill(food) will work.

Here is my code that is correct, but still spits out an error

def compute_bill(food:
    total = 0
    for item in food:
        total += prices[item]
        return total

I am not sure if there is a bug with this lesson or some tiny change that needs to be made but it's really bugging me.

Right answer but error message

A function ends the moment a return keyword is reached

if a return keyword is reached in a loop, the function needs to end, so the loop will break

so, your loop ends in the first run/iteration

so if the user buys multiply products, it gets only charged for the first product

place return outside the loop


i just wanna pass this level so i can continue to the rest of python courses , it's realy important for me , any help ?
we go it , just pass the right algorythm and we ll copy it and move forward


Please copy paste your code to the forum so we can help, providing the right code is against the guidelines


Write your code below!

def compute_bill(food) : 
    total = 0
    for item in food :
        if stock[item] > 0 :
            total += prices[item] 
        elif stock[item] == 0:
            return total
print compute_bill(shopping_list)

AND THIS RESULTED IN 7 , yet it says it gave 0 instead of the correct seven , for 1" banana 1 pear( not orange as we sit in the list earlier) and 1 apple




you have partly the same issue as siniister, the moment an item is not in stock, a return keyword is reached, the function ends immediately (the loop will break)

apple is not in stock, so your function ends with apple, while pear and banana and stock

return total after the loop


what you suggest ?
i change the order of the lsit , or change orange to pear in the list ?
note that the list have orange in it but error message in the image i shared says it should be a pear !


you don't want your function to end in the loop when a product is not in stock, (a return keyword in the loop will end loop and function), you do want to sell the remaining products the customer wants to buy, its a shop, its about selling


def compute_bill(food) : 
    total = 0
    for item in food :
        if stock[item] > 0 :
            total += prices[item]
        elif stock[item] == 0:
            return total
            return total
    for item in stock :
print compute_bill(shopping_list)

the cosule give 7 , yet that message shows up again !


the purpose of our loop, is to calculate the cost of products the customer wants to buy

this mean looping over the products (food) adding the price of items to total, once the loop has successfully added all cost to total, you need to return total

Go run your code in this visualizer to see your return keywords to cause the function to end way to soon


can you give the whole thing to paste and pass forward ! i got the lesson ! i just need to continue i way behind my friends , i need this


i can't, as moderator, i can't breach the guidelines.


alright , thank you any way


make a new topic, maybe someone else can help you better then i can


What I did was returned the total after the for loop. HAHA I was stressing over a whitespace lol.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.