0

When I compile the Python code below, I get IndentationError: unindent does not match any outer indentation level

Files = os.listdir(".")
monthNum = 1
totMonths = 0
for year in range(2003, 2016):
    os.chdir('./'+str(year))
    for month in range(1, 13):
        totMonths = totMonths +1
        if (month < 10):
            monthStr = str(year)+"0"+str(month)
        else:
            monthStr = str(year)+str(month)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
karem
  • 1
  • 1
  • 1

1 Answers1

1

This error is caused when the actual indentation does not match the expected indentation -- a likely culprit is tabs v spaces, and indentation consistency.

For example, the following code would throw this error:

if true:
    return 1
if false:
     return 2

Note that while the true statement is indented by 4 spaces, the false statement is indented by five.

This example would also throw this error, and depending on your text editor could be invisible (hidden chars added for reference):

if true:
....return 1
if false:
⇥   return 2

In this case the true statement is indented using four space characters, while the false statement is indented using a tab symbol.

Along with the IndendtationError you provided, it should give you the exact line the error is occuring on. That said, the sample you provided is small enough that unindenting & reindenting the whole thing should resolve it.

Ceili
  • 1,308
  • 1
  • 11
  • 17