# Compute_bill

#1

``````def compute_bill(food):
total = 0
for food in shopping_list:
total=total+prices[food]
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
#2

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

#3

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
thanx

#4

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

#5

# 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

thanx

#6

#7

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

#8

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 !

#9

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

#10

``````def compute_bill(food) :
total = 0
for item in food :
if stock[item] > 0 :
total += prices[item]
elif stock[item] == 0:
return total
else:
return total
for item in stock :
stock[item]-=1

print compute_bill(shopping_list)``````

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

#11

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

#12

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

#13

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

#14

alright , thank you any way

#15

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

#16

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

#17

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