12.Stocking out. Not returning correct value



Hey guys, its my first time posting, I really wanted to give this a shot without asking for help but im kinda perplexed. Its probably something stupid im missing.
Ive looked at other posts regarding this section and my code looks pretty much identical to the other solutions ive seen. However when passed a list of 1 apple, 1 pear, and 1 banana, Its returning 0 instead of 7

Here is the error message im given.

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

Below is the code i have written

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

Cheers and thank you in advance.


Look at where your return keyword is indented, we want to get the full bill from our loop.


So it was totally something stupid! Thank you for taking a look at it. The indentation in python is going to take a bit of getting use to for sure.


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