Day at the supermarket - Stocking Out - Indentation


#1

https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/stocking-out-?action=resume_content_item

Here’s what I ended up with, which works:

06%20PM

My question is, why does the ‘stock[item]’ decrement need to be indented further like that? If I don’t indent that way, I get the error ‘unindent does not match any outer indentation level.’

Isn’t it part of the ‘if’ block? Does the ‘total’ incrementation create a new block? If so, why? If I remove the incrementation of ‘total,’ the decrement will still work.

Thank you


#2

Did the code shown on the image really work? Because it does not.
All the code in an if statement has to be lined accordingly, that is 1 or more spaces under the if statement, look at the arrow next to the if.
On the image you posted there is an indent before stock[item] which is not correct.

This is correct.

def compute_bill(food):
  total = 0
  for item in food:
    if stock[item] > 0:
       total = total + prices[item]
       stock[item] -= 1
  return total

#3

Hi!

Yes, the block of code as you show in your email was giving me the error, and the indented stock[item] was the only way I could get the code to work.

However, I continued to adjust things after I posted my question and found that if I deleted all whitespace to the left of the line, then use spaces to indent rather than tabs, it finally worked. Is this due to a Codecademy GUI configuration, or does Python require space indentation, or neither?

I was also seeing indentation errors with the return statement in the correct position, after I changed the space indentation of stock[item]. in the correct position, ‘return total’ was causing an ‘unexpected indentation’ error. Again, I changed the indentation to spaces and it worked correctly.

Thanks,


#4

Codecademy’s text editor in python exercises is misconfigured, yes. It should not insert tab characters when pressing the tab key, and it should display tab stops at multiples of 8, not 4 or whatever it’s using.


#5

Hi @mrqrvly,

It is best to post code as formatted text in the forums, rather than as captured images, so that users can copy, paste, and test it. With Python code, this testing may include checking the details of the indentation. In the case of your code, for example, we would check whether the whitespace in the if block is composed of spaces, tabs, or a combination of the two. Make sure that your whitespace is composed of one or the other, rather than both. For advice of how to format code as you post it, see How to ask good questions (and get good answers).

Python editors vary regarding how much whitespace they insert, by default, for each level of indentation and whether they insert tabs or spaces. For most or all of Codecademy’s current Python content, the default is two spaces for each level of indentation. Whenever you start using a particular editor for the first time, you should find out, through trial and error, and documentation if it is available, how it behaves, so that you develop an intuitive knowledge of what sort of whitespace you are getting. Find out, for instance, whether using the tab key literally inserts a tab, or whether it inserts some number of spaces.