A Day at the Supermarket - Stocking Out


#1
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):
  sum_price = 0
  for key in food:
    if stock[key] > 0:
      sum_price += prices[key]
      stock[key] -= 1
    else:
      print key +" is not in stock"
  return sum_price
print sum(shopping_list)

Why doesn’t this print “apple is not in stock” ?


FAQ: Learn Python: A Day at the Supermarket - Stocking Out
#2

Hi @burakbaggins75419051 ,

The compute_bill function will not execute unless you call it, for example …

print(compute_bill(shopping_list))

However, you may need to wait until the final exercise in this lesson to perform that function call.

This statement should be removed …

print sum(shopping_list)