IndentationError: expected an indented block

In documentation terminology, indentation means the space from margin to the begin of characters in a line. In most popular programming languages, spaces or indentation are just used to make the code look better and be easier to read. In Python , it is actually part of this programming language. Because the Python language is a very sensitive language for indentation, it has caused confusion for many beginners. Putting in an extra space or leaving one out where it is needed will surely generate an error message . Some common causes of this error include:
  1. Forgetting to indent the statements within a compound statement
  2. Forgetting to indent the statements of a user-defined function.
The error message IndentationError: expected an indented block would seem to indicate that you have an indentation error. It is probably caused by a mix of tabs and spaces . There are two main reasons why you could have such an error: example 1:
if 1 != 2: print("Not indented!") //print() function not indented
python Error:IndentationError: expected an indented block
The above example fails to indent after the if statement and the output states that you need to have an indented block on line 2.
if 1 != 2: print("indented!") //indented
example 2:
Why am I getting “IndentationError: expected an indented block”
The output states that you need to have an indented block on line 4, after the else: statement

Python block

Here you can see, what follows the colon (:) is a line-break and an indented block . Python uses white-space to distinguish code blocks. You can use spaces or tabs to create a Python block . When several statements use the same indentation , they are considered as a block. Python in fact insists that separate statements use the same indentation in a block. It would complain if you forget to indent when a block is expected, as well as if you use varying indentations.

Indentation in Python

The indentation can be any consistent white space . It is recommended to use 4 spaces for indentation in Python, tabulation or a different number of spaces may work, but it is also known to cause trouble at times. 4 spaces are a good compromise between small indentation (allows greater nesting depth) and large indentation (easier to read). Using only spaces is generally the better choice. Most editors have an option for automatically converting tabs to spaces. If your editor has this option, turn it on.

Tabbed indentation

Tabs are a bad idea because they may create different amount if spacing in different editors . They're also potentially confusing if you mix tabbed indentation with spaced indentation. If you're using an IDE like Eclipse , you can configure how many spaces the IDE will insert when you press tab. It's important to note that most modern IDEs help you keep indentation using tabs or spaces , but since whitespace characters are not visible in some editors, you should take care to properly indent blocks .

Docstring Indentation

This error IndentationError: expected an indented block can also come up if the programmer forgets to indent a docstring. Docstrings must be in line with the rest of the code in a function. Docstring processing tools will strip a uniform amount of indentation from the second and further lines of the docstring, equal to the minimum indentation of all non-blank lines after the first line.
def print_msg(the_msg): """this doctring not indented""" print("Not indented!")
Python IndentationErrors
To fix this issue, indent the docstring .
def print_msg(the_msg): """this doctring is indented""" print("indented!")