Indentation Error in Python
In Python, an Indentation Error occurs when the code is not properly aligned. Since Python relies on indentation to define code blocks, even a small misalignment can prevent the program from running.
- Python enforces indentation to define the scope of loops, functions, conditionals and other blocks of code.
- If your indentation is inconsistent or misplaced, Python will throw an error before it even runs the program.
- Thatâs why this is considered a syntax error (detected before execution).
In many languages like C, C++ or Java, code blocks are defined using braces { }. This means indentation is optional and mainly used just to improve readability.
Unlike C, C++ or Java, Python does not use braces at all. Indentation itself defines the block structure and determines which statements belong together. For example:
if a > 2:
if a < 7:
return "Number is between 2 and 7"
Here, spaces before each line show Python which statements belong to the if block. Thatâs why proper alignment is not optional, but strictly mandatory in Python.
Cause of Indentation Error
Indentation errors usually happen because of small mistakes. Common causes include:
- Misplaced spaces or gaps: e.g., forgetting to indent inside a loop or function.
- Mixing tabs and spaces: Python treats a tab and four spaces differently, which often causes confusion.
- Incorrect indentation in compound statements: e.g., if, for, or while blocks not properly aligned.
- Extra or missing indentation: adding one extra space can throw an error.
Hereâs a program with incorrect indentation:
def check_number(a):
if a > 2:
if a < 7:
return "Number is between 2 and 7"
return "Number is greater than 2"
return "Number is out of the range of 2 and 7"
a = 5
result = check_number(a)
print(result)
Output
IndentationError: expected an indented block
Why did this happen?
- The if statements are not indented under the function definition.
- The inner if also lacks indentation to show it belongs inside the outer if.
- Python cannot guess what you mean, so it stops execution.
- Indentation is the only way to indicate which statements belong together, so any inconsistency breaks logical structure of the code.
Fixing Indentation Errors
Proper indentation not only prevents errors but also improves readability. It makes your code easier for others (and yourself) to understand.
Now letâs fix the indentation properly:
def check_number(a):
if a > 2:
if a < 7:
return "Number is between 2 and 7"
return "Number is greater than 2"
return "Number is out of the range of 2 and 7"
a = 5
result = check_number(a)
print(result)
Output
Number is between 2 and 7
How to Avoid Indentation Errors
To minimize indentation-related issues in Python, it is important to follow consistent coding practices:
- Use a reliable IDE or code editor (such as PyCharm, VS Code, or Jupyter Notebook). These tools highlight indentation errors and often provide auto-formatting options.
- Prefer spaces over tabs. According to the official Python style guide (PEP 8), four spaces per indentation level are recommended.
- Enable âshow whitespaceâ in your editor to make hidden characters like tabs and spaces visible.
- Maintain consistency by not mixing tabs and spaces within the same project.