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:- Forgetting to indent the statements within a compound statement
- Forgetting to indent the statements of a user-defined function.
if 1 != 2:
print("Not indented!") //print() function not indented
output 
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: 
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!")
output 
To fix this issue, indent the docstring .
def print_msg(the_msg):
"""this doctring is indented"""
print("indented!")
Related Topics
- TypeError: 'NoneType' object is not subscriptable
- IndexError: string index out of range
- IndentationError: unexpected indent Error
- ValueError: too many values to unpack (expected 2)
- SyntaxError- EOL while scanning string literal
- TypeError: Can't convert 'int' object to str implicitly
- ValueError: invalid literal for int() with base 10
- IndexError: list index out of range : Python
- AttributeError: 'module' object has no attribute 'main'
- UnboundLocalError: local variable referenced before assignment
- TypeError: string indices must be integers
- FileNotFoundError: [Errno 2] No such file or directory
- Fatal error: Python.h: No such file or directory
- ZeroDivisionError: division by zero
- ImportError: No module named requests | Python
- TypeError: 'NoneType' object is not iterable
- SyntaxError: unexpected EOF while parsing | Python
- zsh: command not found: python
- Unicodeescape codec can't decode bytes in position 2-3