[Solved] IndentationError: expected an indented block after 'if' statement


IndentationError: expected an indented block after 'if' statement

This error typically occurs when Python expects an indented block of code after an if statement, but the block is missing or improperly indented.

For example, consider the following program, where we have an if-statement. The print() statement that is required to be in the if-block is out there at the same level as if-statement.

Python Program

x = 10

if x==10:
print("x is 10.")

Output

  File "example.py", line 4
    print("x is 10.")
    ^
IndentationError: expected an indented block after 'if' statement on line 3

The message printed to the output clearly states that Python interpreter expected an indented block after 'if' statement on line 3.

Solution

Let us provide proper indentation to the statements that are supposed to be in the if-block.

Python Program

x = 10

if x==10:
    print("x is 10.")

Output

x is 10.

Now, the program has run without any IndentationError.

Summary

In this tutorial, we learned how to solve the IndentationError that occurs when we miss the indentation of the if-block inside if-statement.