#12 stocking out


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


Why it not work?


else:, but you did a space before the colon.
hope this helps!

i think you only need 1, because print and return serve kind of the same purpose... but i think you can leave it, won't affect your code.

and try printing this line too.


Im struggling with understanding the logic here. Why do we have to add this line when surely it would just skip over when the stock is 0?


It is just to make the program understand that it can compute the bill as long as it is in the stock. Yes, it will skip over that line once the item is not in stock and it would just return the total of the purchase.

Hope that helps!


I don't think that the total = total line is required. The variable is already defined so you can take it out of the condition. Also, if it still does not work after @kasmotology answer, then maybe there's some problem with your indenting?


Delete the "else:" Part, include "total=total".
Also you don't need "print total" in this, you've already returned the value, it will be ignored, so delete it, too.

When you also delete the last line, it should work: "compute_bill(shopping_list)"


A post was split to a new topic: Stocking out