12. I was struggling a lot with this one too


#1

Hi there. I was struggling a lot with this one too.
This was the code that eventually worked for me:

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

My code loops the 'shopping_list - list' ( I named it food)
It then checks whether the item is in stock or not, if it is --> price of item gets added to total.
Then it checks whether or not the item is still in stock after the purchase, if so the quantity of that item gets decreased by 1.

I ended up using item instead of key after getting a couple of errors that said 'key' was not defined.

Hope this helps.


12. Stocking Out
#2

No need to use second "if" statement.