If you get an "IndentationError expected an indented block" in Python, it means that you have not indented one or more lines of code that should be indented.
In Python, indentation is used to indicate which lines of code belong to the same code block.
Here is an example of how to fix an "IndentationError expected an indented block" in Python:
# this code will cause an IndentationError
if True:
print('Hello, world!')
# this code will not cause an IndentationError
if True:
print('Hello, world!')
In the example above, the first code block will cause an "IndentationError expected an indented block" because the print
statement is not indented.
The second code block does not cause an error because the print
statement is indented, indicating that it belongs to the code block controlled by the if
statement.
To fix an "IndentationError expected an indented block" error, you should make sure that all lines of code that should belong to the same code block are indented by the same amount.
This includes lines of code that are controlled by an if
statement, for
loop, while
loop, or any other control statement that uses indentation to indicate the code block.
In general, you should always use four spaces to indent your code in Python.
This is the standard indentation style in Python and it will help you avoid indentation errors like the one mentioned in this question.
Related tutorials curated for you
SyntaxError: unexpected EOF while parsing
How to fix: IndentationError expected an indented block in Python
Syntax Error: invalid syntax
TypeError: string indices must be integers
ValueError: setting an array element with a sequence
How to fix: Unindent does not match any outer indentation level in Python
TypeError: only integer scalar arrays can be converted to a scalar index
ModuleNotFoundError: No module named 'matplotlib' in Python
ModuleNotFoundError: No module named 'requests' in Python
ModuleNotFoundError: No module named 'cv2' in Python
How to fix: pandas data cast to numpy dtype of object. Check input data with np.asarray(data)
ModuleNotFoundError: No module named 'pandas' in Python