[Solved] IndentationError: unindent does not match any outer indentation level I do not understand anything [closed]


A diagrammed version of the indentation of Night Spirit's code. A red line indicates that the line in question does not align to any previous indentation level

I diagrammed your code. See how the line that’s throwing an error doesn’t line up with any previous indentations? Python doesn’t know what to do with that, so it throws an error. In general, try and make your indentations always the same number of spaces, to avoid this.

Consider these two pieces of code:

a = 3
if a < 5:
        if a < 2:
            print("option 1")
        else:
            print("option 2")

Here, the “else” is regarding the if a < 2 statement, so “option 2” will be printed. Compare that to:

a = 3
if a < 5:
        if a < 2:
            print("option 1")
else:
    print("option 2")

Here, nothing will be printed, because the else refers to the if a < 5 line. Now imagine:

a = 3
if a < 5:
        if a < 2:
            print("option 1")
    else:
        print("option 2")

Which if statement is the else in reference to? It’s ambiguous, and so python says “fix this!” rather than taking a guess.

solved IndentationError: unindent does not match any outer indentation level I do not understand anything [closed]