Stocking Out error....please help


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/stocking-out-?action=lesson_resume

<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

```python
<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
    print(compute_bill(shopping_list))>![chrome_2017-10-04_15-35-13|690x387](upload://q7RxZm2ekDPWQXjZrMBqkPjxMgl.png)

#2

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


#3

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


#4

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


#5

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


#6

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