Python uses the whitespace at the start of a line to determine which statements belong together. Indentation therefore affects how code runs; it is not merely visual formatting. If you see an indentation error, check the block header, the lines beneath it, and whether each later line returns to an indentation level already in use.
How Python indentation groups statements
A line that begins farther to the right belongs to a nested block when the code’s structure calls for one. Lines at the same indentation level are siblings; returning to a previous level closes the deeper block.
As an Amazon Associate I earn from qualifying purchases.
if temperature > 25:
print("It is warm")
print("Consider some water")
print("Done")
The two indented print statements are inside the if body. The final statement returns to the outer level, so it is not part of that body. The colon introduces the block; the indentation marks its contents. See the Python 3.14.8 language reference on indentation.
What “expected an indented block” means
After a block-opening header such as if, for, while, or def, Python expects the block’s body to begin at a deeper indentation level. This code has no indented body:
#1 Best Overall
if temperature > 25:
print("It is warm")
Indent the body so it sits under the header:
if temperature > 25:
print("It is warm")
The error name and reported line can vary with the code and where Python detects the malformed structure. The underlying cause may be on the line shown or on a nearby line that established an unexpected level.
Four spaces is the convention, not a parser requirement
PEP 8, the Python style guide, recommends four spaces per indentation level and says, “Spaces are the preferred indentation method.” Four spaces make nested blocks easy to read, but the parser’s job is to recognize consistent indentation levels, not to require exactly four spaces. PEP 8 also allows tabs when maintaining code that already uses tabs. For new code, use spaces consistently. Read PEP 8’s indentation guidance.
Rank #2
Spaces, tabs, and TabError
Python expands tabs to tab stops when calculating indentation; each tab advances to the next multiple-of-eight position. As it reads code, it tracks the active indentation levels. An increase creates an INDENT token; a decrease must return to a level already established, producing one or more DEDENT tokens. A decrease to an arbitrary position is not a valid way to close a block.
Free tools Windows power users keep installed
One-click scans. No signup required.
If tabs and spaces are mixed so that the indentation is ambiguous, Python raises TabError. Code can look aligned in an editor while containing different whitespace characters, so visual appearance alone may not reveal the problem. Use one indentation style throughout the file, and replace suspicious leading whitespace consistently rather than adjusting lines by eye.
A practical way to find the mistake
- Locate the block header. Find the relevant line ending in a colon, such as an
if,for,while, ordefstatement. - Check the first body line. It should start at a deeper level than the header. If it does not, indent it and the other statements that belong in the body.
- Compare sibling statements. Statements meant to be part of the same block should line up exactly. Check that an
else,elif,except, orfinallyaligns with its matching opener, and that the clause’s own body is indented beneath it. - Check every dedent. When a line moves back to the left, align it with a level already used by an enclosing block. Do not choose an in-between position.
- Inspect nearby lines and whitespace. The reported line is a useful clue, but a preceding line may have created the unexpected level. If code was copied from elsewhere, replace its leading indentation consistently with spaces.
Learning Python beyond indentation
For a broader, hands-on introduction, No Starch Press lists Python Crash Course, 3rd Edition by Eric Matthes as a 552-page print book dated December 2022 (ISBN-13 9781718502703). Its publisher describes it as covering Python basics, exercises, projects, and troubleshooting. It is an optional learning resource, not required to understand indentation. See the publisher’s listing.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




