IndentationError: unindent does not match any outer indentation level

Table of Contents What is indent?What is indentationError?What is “indentationError: unindent does not match any outer level ”?Conclusion: What is indent? Indents refer to whitespaces or blank spaces. Indents are used in programming to make …

unindent does not match any outer level

What is indent?

Indents refer to whitespaces or blank spaces.

Indents are used in programming to make code look nice and readable in other programming languages such as java, c, c++ indents are not part of code or make no difference in code.

We use indents to make it look clear and easily readable.

But in python, indents play an important role in code execution because indents are part of programming in python.

Missing an indent or adding an extra indent can cause an indentation error.

What is indentationError?

IndentationError arises in the python program when you miss an indent or add an extra indent or intermix the spaces and tabs.

As python indents refer to four-whitespaces, if you add somewhere in code five-whitespaces or three-whitespaces, or when you used through-out the code spaces and in-between somewhere you used tab to give indent, then it will cause IndentationError.

What is “indentationError: unindent does not match any outer level ”?

The “IndentationError: unindent does not match any outer indentation level” occurs when you add an extra blank space or white space, or when you miss whitespace, or when you inter-mix the tabs and spaces.

Let’s understand it better with the help of examples:

ERROR Example:

#python indentationError
#indentationError: unindent does not match any outer indentation level

a = 9
b = 8
if b > a:
        print("B is greater than A")
elif a == b:
        print("A and B are equal")
    else:
        print("A is greater than B")

Output:

File "<tokenize>", line 7
    else:
    ^
IndentationError: unindent does not match any outer indentation level

In the above example, we encountered an “IndentationError: unindent does not match any outer indentation level” in line7.

Solution:

To solve this IndentationError, we will check that if we have indented the code properly or not, and we also check whether we intermixed the spaces or tabs.

If anything you find, then correct it to avoid this error.

Let’s solve the error example and see what was encountering the error.

Solution Example:

#indentationError in python
#unindent does not match any outer indentation level

a = 9
b = 8
if b > a:
        print("B is greater than A")
elif a == b:
        print("A and B are equal")
else:
        print("A is greater than B")

Output:

A is greater than B

In the above example, we have solved the error example and removed the error.

By just removing an extra indent(blank spaces) from line7.

Solving IndentationError is a very simple yet complex task, though, because we have to revise the whole program to find where we added an extra-indent or missed an indent.

But once we find it, we can remove it within seconds just by properly indenting the code.

Conclusion:

In this article, we discussed the “IndentationError: unindent does not match any outer indentation level” how can we solve this error with an easy programming example.

Suggested Read: 500 Internal server error

Leave a Comment