How to Fix IndentationError: expected an indented block

Diagram of How to Fix IndentationError: expected an indented block

Diagram

To fix the IndentationError: expected an indented block error in Python, “indent the code block correctly to align it with its surrounding code.”

IndentationErrorexpected an indented block error occurs in Python when the code is not indented correctly.

In Python, whitespace (indentation) is significant and determines the scope of a code block.

An “IndentationError: expected an indented block” means a block of code that is supposed to be indented at all or not indented enough, leading the interpreter to believe that the block of code is not part of the current scope.

Reproduce the error

def compare(num):
 if num >= 1:
 print("It is positive number")
 elif num < 0:
 print("It is negative number")
 else:
 print("It is zero") 


compare(1)

If you run the above file, your output looks like the one below.

 File "/Users/krunal/Desktop/code/pyt/database/app.py", line 3
 print("It is positive number")
 ^
IndentationError: expected an indented block

Other causes of the error

The causes of the IndentationError: expected an indented block error include:

  1. Incorrect indentation levels: The code is not indented to the proper level
  2. Mixing tabs and spaces for indentation causes inconsistencies, leading to the error message.
  3. Improper use of white space characters: Using extra spaces or the wrong type of white space character can result in an error.

How to Fix IndentationError: expected an indented block

Python cares about indention

In Python, indentation replaces the keyword begin / end or { } and is therefore necessary.

This is verified before the execution of the code; therefore, even if the code with the indentation error is never reached, it won’t work.

def compare(num):
 if num >= 1:
 print("It is positive number")
 elif num < 0:
 print("It is negative number")
 else:
 print("It is zero")


compare(1)

Output

It is positive number

And the IndentationError is successfully resolved.

Let’s see the example in which we don’t write anything after the first if statement.

def compare(num):
 if num >= 1:
 
 elif num < 0:
 print("It is negative number")
 else:
 print("It is zero")


compare(1)

Output

File "/Users/krunal/Desktop/code/pyt/database/app.py", line 4
 elif num < 0:
 ^
IndentationError: expected an indented block

After Python reads the if statement, it expects to see at least one child line following it. However, since the next non-empty line reads it is the elif statement, which means the if statement has no children, Python reports that it expected some indented lines.

To fix this IndentationError, either place at least one line of code as the if statement’s child or remove them entirely.

def compare(num):
 if num >= 1:
   print("It is positive number")
 elif num < 0:
   print("It is negative number")
 else:
   print("It is zero")


compare(1)

If you run the above code, you will get the expected output.

Python unexpected indent

Python throws an IndentationError when it finds a line indented as if the line had some parent line, but it couldn’t get any lines above to be its parent.

For example, you face this unexpected indent error when a line is indented by one or more spaces more than the previous line, and the previous line isn’t def, if, elif, else, for, or while loop.

That is it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.