Stocking Out Error


I keep getting the following error when attempting to resolve this task:

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

It looks to be inputting values for my food argument, even though that hasn’t been defined. I’m not sure how it is pulling those in - regardless, I don’t see anything wrong with my code in terms of logic/indentation.

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


There’s additional code that is used to test your code, yes. That’s how codecademy determines whether or not to pass you. The error message that you’re given tells you enough to test the same thing by creating a list with those items and calling your function with that, and checking the result – so there’s an easy way that you can verify whether this is indeed happening, and then you can dig deeper to find out how you got that result (such as by adding print statements in your function to print out what’s being done step by step)


The problem is their answer is exactly the same as mine, albeit they iterate over “item” in the FOR loop instead of “x”.

Really have been disappointed with the quality of some of these questions. Indentation errors all over the place, even if it looks right in the editor, but then you paste it into notepad and it’s completely off, and now this kind of thing.


The variable name doesn’t make any difference, what does make a difference is whether you iterate over something that can be iterated over, such as a list, or something that can’t be, such as an int.

The editor’s behaviour is definitely codecademy’s fault though.


I get the same bogus error in the editor about an unexpected indent even after redoing every indent line. It looks perfect in the editor, then when I copy and paste into a text editor there are bad indents where 3 levels worth of space characters have been turned into a tab, and so on.


just curious, why we have to use “stock[x] = stock[x] - 1” in this case instead of stock[x] -= 1


That’s news to me, so you’d have to refer to whatever tells you that (if you’re including facts in your question you need to back them up a bit if other people don’t consider them facts)


you mean, screen shot of the error info?


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