Diagram
IndentationError: expected an indented block error occurs when the code is not indented correctly.
To fix the IndentationError, indent the code block correctly to align it with its surrounding code.
The whitespace (indentation) is significant and determines the scope of a code block. This error is common due to its reliance on indentation to define code blocks rather than using braces or other delimiters, as in many other programming languages.
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
How to Fix IndentationError?
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.
If Statements
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.
I am using Visual Studio Code and I installed the autopep8 extension:
After installing, if you right-click on the code area, you will get a menu with this option: Format Document
And it will do the job!
Extension of if-else case
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, 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. Any empty blocks contain a “pass statement” or a suitable placeholder.
For loop
For loop can throw an IndentationError message whenever you do not indent your code block properly.
# Define a list of items
fruits = ["apple", "banana", "cherry"]
# Iterate over the list
for fruit in fruits:
print(fruit)
Output
IndentationError: expected an indented block after 'for' statement
To fix it, ensure the code inside for loop is properly indented!
# Define a list of items
fruits = ["apple", "banana", "cherry"]
# Iterate over the list
for fruit in fruits:
print(fruit)
Output
apple
banana
cherry
Unindented docstring
Docstrings document the purpose and usage of functions, classes, methods, and modules. An indentation is not just a matter of style but a syntactical requirement, and this applies to docstrings as well.
When adding docstrings, they should be properly indented to align with the code block they are documenting.
def my_function():
"""
This is a docstring for my_function.
It explains what the function does.
"""
# Function implementation goes here
pass
That’s it!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.