Why is compute_bill returning the wrong value?



Why is compute_bill returning the wrong value?


A common issue here is indenting the return statement inside of the for loop, which causes the loop to only be able to possibly run one time before the function returns and exits.
If your code looks something like the code below, unindent your return statement to be on the same level as the for, not inside of it.

for item in my_list:
  my_var += 10
  return my_var

FAQ: Learn Python: A Day at the Supermarket - Making a Purchase

I have been struggling with this the last few exercises so I figured I’d post. How do you determine how you define “things” in lists/dictionaries when it comes to loop statements? For example, the solution for this problem says " for item in food". How did you come to the conclusion of using the term item? In previous examples, “for food in prices” was used. How was it determined “food” should be used? Thanks.


the for in loop (for variable in list) declares (names) a variable within the for loop, you are free to pick your own variable names, except for the reserved words in a programming language

however, its recommend to give variable logic/descriptive names. This is something that comes with time and experience.

Whoever wrote the course decided on the variable names used, because (s)he thought these names fitted best.


I also have a question on this exercise. My code (below) is coming back rejected.

def compute_bill(food):
  total = 0 
  for p in prices:
    total += prices[p]
  return total

The tip below the output says
compute_bill(['apple']) returned 10.5 instead of 2

Wasn’t the point of the exercise to compute all of the food items on the list (Which would give us $10.50). It doesn’t make sense that the computer is trying to pass ‘apple’ as an argument through this function since we already know its price is $2.

Please help. Thanks.


no, the point of this exercise was to make a function which could calculate the total cost of a customer purchase.

that is why the function has a parameter, so we can test different “customers” (by passing various shopping lists as argument on function call)


This is what I came up with and it passed:

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

print compute_bill(shopping_list)

From my understanding, we’re to assume the customer only wants 1 of each item on their list. 7.5 was printed to my console, which equals the combined cost of 1 apple, 1 orange and 1 banana as defined in the dictionaries.

Did you change the values of the keys in your dictionaries? These lessons don’t take kindly to mutations in the provided code, or the use of definitions aside from the ones provided in the lesson outline.

Your function looks essentially identical to mine, so I don’t imagine that’s the problem.


we could add multiply function calls to represent different customers:

print compute_bill(shopping_list)
print compute_bill(['pear', 'apple'])
print computer_bill(['pear', 'pear', 'pear', 'orange', 'orange'])

no, there is a one difference between your and kmonroe25 code. Study the for loop closely. He is looping over prices, and not using his food parameter. So if you run his code and add a bunch of function calls you will see the function always returns 8.5, regardless of what customer (function call) wants to buy