Stocking Out error....please help



<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
it’s only printing 4

<What do you expect to happen instead?>
run without error

<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 stock[item] > 0:
            total += prices[item]
            stock[item] -= 1
        return total


by default a function returns None at the end, if we want to return something else at the end of the function, we can use the return keyword. So when a return keyword is reached, the function ends

so your program ends in the first iteration of the loop of your loop, but to compute the correct bill, the loop needs to make all its iteration, how can we fix this?

don’t call the function, it causes problem with exercise validition


Sorry.I didn’t get what you mean…should I remove the last line or the return keyword?? :thinking:


in order to get the correct total, the loop needs to make all its iterations, to achieve this return must be placed outside the loop, so the only thing you should remove is some indent


delete the indent before return and your code is good to go


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