Stocking out: Cannot get any code snippets to work


Hi all,

I am trying to get stocking out to work but it will not take anything I give it. Here is my code:

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

stock = {
  "banana": 6,
  "apple": 0,
  "orange": 32,
  "pear": 15
prices = {
  "banana": 4,
  "apple": 2,
  "orange": 1.5,
  "pear": 3

# Write your code below!
def compute_bill(food):
  total = 0
  for item in food:
    if item > 0:
      total += prices[item]
      stock[item] -=1
      return total
    print total

I get the following error:

calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 2 instead of the correct 7

I even tried pasting other peoples solutions and nothing works. Clearly I am being an idiot. Can you please help me see where :slight_smile:


by default, a function returns None at the end

we can return something else at the end of the function by using a return keyword

given return is the last thing a function does, a function ends the moment a return keyword is reached

so currently, your function ends in the first iteration of the loop

to calculate the correct total/bill, the loop need to make all its iterations


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.