[Solved] IndentationError: unindent does not match any outer indentation level
IndentationError: unindent does not match any outer indentation level
An IndentationError: unindent does not match any outer indentation level
typically occurs when there is an inconsistent or mismatched indentation level in your code, particularly when dedenting (removing indentation) doesn't match the level of outer indentation.
Here's an example that can cause this error:
Python Program
x = 5
if x == 5:
print("x is 5.")
print("end of if") # Inconsistent indentation level
Output
File "example.py", line 5
print("end of if") # Inconsistent indentation level
^
IndentationError: unindent does not match any outer indentation level
In this example, the print("end of if")
statement is expected to be at the same indentation level as the print("x is 5.")
statement. However, it's indentation is not matching with that of the previous statement, which leads to the IndentationError
.
Solution
To fix this error, ensure that all the lines in the block are at the same level of indentation. Here's the corrected version:
Python Program
x = 5
if x == 5:
print("x is 5.")
print("end of if") # Inconsistent indentation level
Output
x is 5.
end of if
Now, the program has run without any IndentationError
.
Summary
In this tutorial, we learned how to solve the IndentationError
that occurs when we provide an inconsistent indentation for the statements inside a block of code.