Online Tutorials & Training Materials | STechies.com
Register Login

How to Fix Indentation Error in Python: unindent does not match any outer indentation level

|| 0

How to Fix Indentation Error in Python: unindent does not match any outer indentation level
Stechies

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 determine what block of code a statement belongs to. And the end of statement is marked by 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.

indentationerror: unindent does not match any outer indentation level-1

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 to avoid such indentation errors.

indentationerror: unindent does not match any outer indentation level

This type of error occur when we assign extra indent to identical code block, due to extra indent python compiler is not able to recognize identical code blocks and it throws error.

indentationerror: unindent does not match any outer indentation level-2

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 example “if” and “elif” statements are assigned with no indent where as “else” statement (see line no 7) which is belong to “if” statement, assigned with extra indent. Due to extra indent python compiler was not able to recognize “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")

Error: IndentationError: expected an indented block

indentationerror: unindent does not match any outer indentation level-3

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: unindent does not match any outer indentation level-4

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 space extra 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. 

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