Making a purchase

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

compute_bill([‘banana’, ‘apple’, ‘orange’, ‘pear’]) returned 4 instead of 10.5

Why don’t work? Help me please.

Making a purchase | A day at the supermarket

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. But given return is the last thing a function does, the function ends when a return keyword is reached

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

with this information i just provided, how could we overcome your problem?


This problem in indents: “for” and “return”…
Thank’s! It’s ok. Now next :slight_smile: )


yep, but its important to understand what the significance of this indent is, then you can safely move on to the next exercise :slight_smile:


Sorry, my English is not good.
But, how I understand, because the return command orders the function to be “computable” and then it has to be indented with the function and not with other commands.


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