It is a common programming practice to indent code. Indentation uses spaces or tabs to separate the code for better visualization. This is important for code readability.
In Python, indentation is part of the syntax.
def foo()
print("foo") // 1st level indentation
if True:
print("True") // 2nd level indentation.
print("Source") // No Indentation, part of source.
The code snippet above describes how to indent code in Python. Failing to adhere to these principles can lead to the “IndentationError: expected an indented block” error.
When we say indentation, it is basically any consistent white space. However, it is common to use 4 spaces for indentation or apply tabulation to the code.
To resolve this error, check the following things:
else:
part of an if-condition
, and check if the indentation is missing after conditions, loops, etc.x = 5if x == 2:print("x is 2") #indentedelse:print("x is not 2") #unindented
x = 5if x == 2:print("x is 2") #indentedelse:print("x is not 2") #indented
def print_msg(the_msg):"""this doctring not indented"""
Free Resources