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


IndentationError: expected an indented block after 'for' statement

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

For example, consider the following program, where we have a simple For loop iterating over a range of numbers, and printing each number to standard output. But, as you can observe the body of For loop is not indented. But anyways, lets us go ahead and run the program.

Python Program

for i in range(10):
print(i)

Output

  File "example.py", line 2
    print(i)
    ^
IndentationError: expected an indented block after 'for' statement on line 1

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

Solution

Let us provide proper indentation to the statements that are supposed to be in the For loop.

Python Program

for i in range(10):
    print(i)

Output

0
1
2
3
4
5
6
7
8
9

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 for the body of For loop statement.