A day at the supermarket | Stocking out


#1
Hi All,

This is my error message :smile:
 #python::tag calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 4 instead of the correct 7 Please see my code below and perhaps advise how this can be fixed.Thanks in advance!

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

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 food in shopping_list:
    if (stock[food] > 0):
      total += prices[food]+total
      stock[food] -= 1
    return total

#2

a function returns None by default at the end of the function

if we want to return something different at the end of your function, we can use the return keyword. So a return keyword will end the function

so your function ends in the first iteration of the loop, but to get the correct total, the loop needs to make all its iterations, not just one


#3

You need to unindent the return line to match the indentation of the for line. It’s currently inside the for loop so it returns after the first iteration through the loop instead of after the loop has finished


#4

Thanks Stetim94.I have specified the return keyword at the in my function.Thanks for the advice though.


#5

Hi Ninjamoose91.I have checked the indentation previously and in fact checked again after I read your reply although I am still receiving the error.


#6

Does your code still look the same as what you posted?


#7

Yes Ninjamoose91//\


#8

Ok,

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

If you draw a straight line up from the front of the word return, it matches the front of if. It needs to match the front of for. You need to move the return back. Also the line total += prices[food]+total is going to double total and add the item price every time. total += prices[food] is all you need, the += adds whatever is on the right to the variable to the left


#9

yes, that is the problem. your return keyword ends the function in the first iteration of the loop, but to get the correct total, the loop need to make all its iterations, so return should be placed outside the loop


#10

I have updated the code so that it is correct.Please see below

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

#11

its not correct, look:

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

print compute_bill(['apple'])
print compute_bill(['orange'])

i added two function calls (remove them after you are done) which represents two customers, the first customer buys an apple, the second an orange

yet they both get billed for: 2 bananas, 2 oranges and a apple

But that is not what they bought.

Please correct this problem, the solution has to with the function parameter and what you are looping over

remember: arguments at function call (['apple'] and ['orange']) gets passed into the function parameter

edit: this is just a vizualization of the problem, yuo still have to code the solution


#12

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