My code isn't working! **FIXED**

https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/stocking-out-?action=resume_content_item
stock = {
“banana”: 6,
“apple”: 0,
“orange”: 32,
“pear”: 15
}

prices = {
“banana”: 4,
“apple”: 2,
“orange”: 1.5,
“pear”: 3
}

Write your code below!

#defines the function
def compute_bill(food):
total = 0
for item in food:
#loops through each item
if stock[item] > 0:
# if the stock is more than 0 then add price of item to total and minus 1 from stock
total = prices[item] + total
stocks[item] -= 1
return total

Hi @bisharp ,

You have a dictionary named stock. Make sure that within the for loop you refer to it by that name, using the exact same spelling wherever you attempt to access it.

Oh, thanks, but I already fixed it. The error is “unindent does not match other indentation levels”.

Since your posted code is not formatted, users cannot see its indentation, making it difficult to help. Does the error message specify a line number where the problem occurred?

Yes, the error message says its on line 26, the stock -= 1 and the arrow is pointing at 1. BTW, how do I format the code?

if stock[item] > 0:
total = prices[item] + total
stocks[item] -= 1

2 Likes

Thanks, @oduffy, for the helpful information.

@bisharp, the problem may be that you used both spaces and tabs for indentation. In a Python program, you should use either spaces or tabs, and not both. On Codecademy exercise pages, uses spaces for indentation.

If you used a tab for indentation on one or both of these lines …

total = prices[item] + total
stocks[item] -= 1

… then after you format your code, the indentation might not look the same as it did in the Codecademy editing window. To correct a problem with a tab in indentation, you can remove all the indentation from the problematic line, then redo the indentation, using only spaces.

We assume you have already corrected the problem with the dictionary name here …

stocks[item] -= 1

Yes, thank you! I have fixed it by adding 2 tabs, but I still didn’t understand the problem.

Hi @bisharp

Following is your original compute_bill function, formatted …

def compute_bill(food):
  total = 0 
  for item in food:
  #loops through each item
    if stock[item] > 0:
    # if the stock is more than 0 then add price of item to total and minus 1 from stock
    	total = prices[item] + total
      stocks[item] -= 1
  return total

Note that the indentation of these two lines looks strange …

    	total = prices[item] + total
      stocks[item] -= 1

That is because the indentation of the first of those lines consists of four spaces followed by a tab. You can confirm that by gradually selecting the whitespace at the beginning of the line.

Instead, the indentation should consist of six spaces, as is the case with the second of those two lines. The indentation of those two lines must match.