Online Tutorials & Training Materials | STechies.com
Register Login

How to Fix IndentationError in Python: Unindent does not match any outer indentation level

09 Aug 2019 12:35 pm || 0

What is Indentation in Python?

In layman language, an indentation or indent is a void space at the beginning of a line which indicates to start of a new statement. In most other programming languages, we use curly braces “{”, “}” to determine what block of code a statement belongs to but in python, we use indentation to decide which block of code a statement belongs to. And the end of the statement is marked by a newline character “n”.

In python, we don’t use indention just for the beautification of code or to make the code look pretty.

In python, when you do indent whether it one space, two space and so on for the first line, it must be same for the rest of the lines. An error will occur if you don’t follow the proper indentation.

In the picture shown below, you can see there are three block code, and each block code have an identical indentation.

What is Indentation

In this article, we explain types of indentation error with diagram, which can help you to understand the importance of indentation in python and how to avoid such indentation errors.

IndentationError: unindent does not match any outer indentation level

This type of error occurs when we assign extra indent to an identical code block, due to additional indent python compiler is not able to recognise similar code blocks, and it throws an error.

IndentationError: Unindent does not match any outer indentation level

Example:

a = int(input("Please enter an integer A: "))
b = int(input("Please enter an integer B: "))
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 "t.py", line 7
    else:
        ^
IndentationError: unindent does not match any outer indentation level

In the above code example “if” and “elif” statements are assigned with no indent whereas “else” statement (see line no 7) which is belong to “if” statement, assigned with an extra indent. Due to additional indent python compiler was not able to recognise “else” statement (line no 7) and throw the indentation error ’unindent does not match any outer indentation level’.

Correct Code:

a = int(input("Please enter an integer A: "))
b = int(input("Please enter an integer B: "))
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")

IndentationError: expected an indented block

IndentationError: expected an indented block

Error:

a = int(input("Please enter an integer A: "))
b = int(input("Please enter an integer B: "))
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 "t.py", line 4
    print("B is greater than A")
        ^
IndentationError: expected an indented block

In the above example, as you can see, we have assigned different indent to condition at line 4 and condition at line 6 within the same statement. That’s why it throws the error.

Solutions:

a = int(input("Please enter an integer A: "))
b = int(input("Please enter an integer B: "))
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")


IndentationError: unexpected indent

IndentationError: unexpected indent

Example:

a = int(input("Please enter an integer A: "))
b = int(input("Please enter an integer B: "))
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 "t.py", line 5
    elif a == b:
    ^
IndentationError: unexpected indent

In the above example, as you can see at line no. 5, we have assigned some extra space indent which make this block code separate(not identical) from other block codes.

Solutions:

a = int(input("Please enter an integer A: "))
b = int(input("Please enter an integer B: "))
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")

TabError: inconsistent use of tabs and spaces in indentation

This type of error occurs when you use tab and white space both for indentation in your python code.

To avoid this type of error, you must use either only space or tabs no mix-ups.

Note: This type of error occurs in older version of python3

Example:

a = int(input("Please enter an integer A: "))
b = int(input("Please enter an integer B: "))
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:

TabError: inconsistent use of tabs and spaces in indentation

Solution:

1. Add given below line at the beginning of code

#!/usr/bin/python -tt

2. Python IDLE

In case if you are using python IDLE, select all the code by pressing (Ctrl + A) and then go to Format >> Untabify Region

TabError: inconsistent use of tabs and spaces in indentation-1