#12 stocking out


#1

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

compute_bill(shopping_list)

Why it not work?


#2

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.


#3

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?


#4

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!


#5

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?


#6

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)"


#7

A post was split to a new topic: Stocking out


#9