Can someone explain why I'm getting an indentation error with this problem?


I’m trying to be efficient with code and I think that’s where my mistakes are coming from - not understanding when certain tools are applicable and when they aren’t. Below is the code I tried to run:


> 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


And I receive the following error:

  File "python", line 23
    stock[item] 	-= 	1
IndentationError: unindent does not match any outer indentation level


The solution code has everything formatted the same way, except for the adding to price and reducing from stock. The solution has it called as:

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

I feel like I’ve missed a rule to when the “+=” and “-=” formats can be used. And I think I’ve used them in previous problems dealing with lists, but maybe I’m misremembering? Any insight?


Can’t see your indentation, but perhaps you’ve got both tabs and spaces in your code

'	'  <- tab character

In any case, +=/-= don’t cause intendation errors.


I didn’t realize there was a distinction between the two, whenever I tab over for formatting it backspaces like it was just another space character. I’ll give that a go and make sure I only have spaces, no tabs. Thanks!

On a side note, how did you format your reply to format and display the code? My post would have seemed more legible if I could figure that out, I’m sure.

Thanks again!


There’s a button in the editor, or manually put ``` before and after
You’re probably familiar with ascii encoding, space is 32, tab is 9, totally separate. The tab key is totally fine to use, but your editor should interpret it as “indent” not “put a tab character here”


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