# 12.Stocking Out

#1

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7

Check if theres a item in stock and after that subtract 1 from the amount of the item inside the stock

``````shopping_list = ["banana", "orange", "apple"]

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

def compute_bill(food):
total = 0

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

#2

Please refresh the browser and make sure that you are not executing the function (for example `print compute_bill(shopping_list)`).

#3

It was a small typo. i corrected it now lol, but anyways ty for the help

#4

def compute_bill(food):
total = 0

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

I did all the same except "else:" and it displays like below.

^
IndentationError: expected an indented block
[Finished in 0.035s]

I don't get it why. Could you explain it why?

#5

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

Hope this is helpful! Please dont forget to use the right indentation it is important.

#6

Because you need to return the total if(condtion) is true not in the contrary case and your "return total" need to be in else statement in your code.

#7

This is like your code should look:
for key in food:
if(stock[key] > 0):
total += prices[key]
stock[key]= stock[key]-1

#8

#9

calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 9 instead of the correct 7
def compute_bill(food):
total = 0
for item in food:
total += prices[item]
if (stock[item] > 0):
total += prices[item]
stock[item] = stock[item]- 1