Stocking Out


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

I keep getting this error: calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7

What is wrong with my code? I simply don’t know?


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. Thus, the function ends when the return keyword is reached (given return happens at the end of function)

so your function ends in the first iteration of the loop, because the return keyword is reached


I still don’t get it. I am supposed to return total right


I get it. Indentation. Thanks


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