How to Solve IndentationError in Python

Python indentation is used for indicating the blocks of the code. In C and C++, use the { } curly braces for splitting the codes.

Why IndentationError in Python occurs?

The IndentationError in Python occurs when the spaces or tabs are not appropriately placed. IndentationError is a standard error that occurs with just an exception indicating the user.

Python indentation denotes the blocks of code that belong to a control structure, such as a `for loop` or an `if` statement. If the indentation of a code block is incorrect, the Python interpreter will raise an `IndentationError`  and cannot execute the code.

The IndentationError is thrown when the compiler cannot understand the code blocks. There will not be an issue if the interpreter does not find any issues with the spaces or tabs.

Type of IndentationError

There are three types of indentation errors. 

  1. Expected an indented block
  2. Unexpected Indent
  3. Unindent does not correspond to any of the outer indentation levels

IndentationError: Expected an indented block

a = int(input("Enter the first input: "))
b = int(input("Enter the second input: "))

if(a > 0 and b > 0):
c = a + b
print(c)
else:
print(0)

Output

IndentationError: expected an indented block

You can see that we got an output: IndentationError: expected an indented block. 

In this example, the code inside the ifstatement is indented correctly, but the code below it is not indented at all. This will cause the Python interpreter to raise an IndentationError when it tries to execute the code because the code below the if statement is not considered to be part of the if block.

Also, we have to give an indentation in the line c = a + b and print(c). The block of code within if, else, for, while, def… It should be appropriately indented to reduce this error. So we should add an indentation for the block inside if and else statements.

a = int(input("Enter the first input: "))
b = int(input("Enter the second input: "))

if(a > 0 and b > 0):
  c = a + b
  print(c)
else:
  print(0)

Output

Enter the first input: 19
Enter the second input: 21
40

IndentationError: unexpected indent

a = int(input("Enter the first input: "))
  b = int(input("Enter the second input: "))
c = a + b
print(c)

Output

IndentationError: unexpected indent

This program raises an exception called the IndentationError: unexpected indent. This is because we have given indentation for b. But there, the indentation should not be used. The compiler thinks of this as a separate block and gets confused about why there is an indentation. Hence this error is raised.

To solve IndentationError: unexpected indent,  remove the indentation where it should not have. For example, if you over-indent the code, you might face the IndentationError: unexpected indent.

a = int(input("Enter the first input: "))
b = int(input("Enter the second input: "))
c = a + b
print(c)

Output

Enter the first input: 19
Enter the second input: 21
40

Unindent does not correspond to any of the outer indentation levels

a = input("Enter the first input: ")
b = input("Enter the first input: ")

if(a > 0 and b > 0):
  c = a + b
print(c)

else:
 print(0)

Output

IndentationError: unindent does not match any outer indentation level. 

Run this program, and it raises an error known as the IndentationError: unindent does not match any outer indentation level. For example, the print(c) statement has been indented but does not match the previous level.

There is no block matching that indentation, so this error is raised. Therefore, we have to give proper indentation to segregate different blocks.

The error will be raised if we give an improper indentation like this.

To solve IndentationError, give proper indentation to the program.

Use the proper indentation to solve Unindent does not correspond to any of the outer indentation levels error.

How to Solve IndentationError in Python

To solve IndentationError in Python, ensure that the code inside each control structure is indented correctly. In Python, the correct indentation for a code block is four spaces.

Verify that it maintains proper indentation in the overall program. For example, we check the program line by line for indentation errors. If we check the program, then this error can be solved quickly. 

There are three types of indentation; we have to take care of those three types.

That’s it.

Leave a Comment