Why Python Throws an IndentationError When Code Looks Perfectly Aligned

Python evaluates raw characters rather than visual column width. A visually flawless block that triggers an IndentationError almost always suffers from mixed tabs and spaces or hidden unicode characters copied from a worksheet.
When Python raises an IndentationError or TabError on a line that looks perfectly aligned to the human eye, the cause is never a visual layout problem. It is a byte problem. Python parses raw character codes, not pixel positions on a screen. If one line begins with four standard space characters (ASCII 32) and the next begins with a single horizontal tab character (ASCII 9), an editor may render them at the exact same visual indentation level, but Python 3 sees two incompatible whitespace conventions and halts execution.
Visual Alignment vs Byte Integrity
In most programming languages, whitespace is decorative: the compiler relies on curly braces {} or keywords like begin and end to delimit blocks of code. Python uses indentation to define syntactic scope. To maintain predictability, Python 3 explicitly disallows mixing tab characters and space characters within the same block.
The trouble in beginner classrooms is that code editors are configurable display engines. By default, environments like Thonny, Mu, or Visual Studio Code can display a tab character using a visual width of four spaces. To a learner—and to a teacher glancing over their shoulder at a shared screen—both lines appear to sit cleanly at column 4:
- Line 1:
[Space][Space][Space][Space]print("Motor running") - Line 2:
[Tab]time.sleep(1)
Both render identically in monospace fonts. Yet the Python interpreter encounters two completely distinct ASCII bytes. Python 3 interprets this mismatch as an ambiguous indentation level and raises TabError: inconsistent use of tabs and spaces in indentation or a generic IndentationError: unindent does not match any outer indentation level.
The Worksheet Pipeline: The Non-Breaking Space Trap
An even more frustrating classroom failure mode occurs when learners copy example code from digital handouts, Microsoft Word documents, Google Docs, or exported PDFs. Modern word processors do not treat whitespace as plain text. To keep layout formatting stable across page margins, word processors silently replace consecutive spaces with non-breaking spaces (Unicode character U+00A0, or byte 0xC2 0xA0 in UTF-8).
When a learner highlights that code snippet and pastes it into Thonny or Mu, those non-breaking spaces come along for the ride. To human eyes, U+00A0 is completely indistinguishable from standard ASCII space U+0020. But Python’s lexical scanner rejects it immediately:
SyntaxError: invalid non-printable character U+00A0
Or, if the editor attempts to treat it as an identifier, it throws an unhelpful IndentationError. A learner can delete and re-add a space manually on that line without solving the problem if the surrounding lines still contain the copied non-breaking character.
Configuring Beginner IDEs to Expose the Invisible
The solution is not to tell learners to look closer; it is to configure the editor to render whitespace visible. When whitespace characters have visible glyphs, learners can debug their own indentation errors within seconds.
Configuring Thonny
Thonny is widely used in South African high school computer labs, maker spaces, and coding clubs due to its lightweight memory footprint. To fix whitespace ambiguity across a lab:
- Navigate to Tools > Options (or Thonny > Preferences on macOS).
- Select the Editor tab.
- Tick the box labelled Show whitespace characters. Spaces will now appear as subtle grey dots (
·) and tabs will appear as faint arrows (→). - Under the same tab, ensure Indent with spaces is checked, and set Tab size to
4. When a learner presses theTabkey, Thonny will insert four standard ASCII space characters instead of a tab byte.
Configuring Mu
The Mu editor is intentionally minimalist for micro:bit and CircuitPython environments, meaning it hides advanced preference panels. However, Mu strictly enforces four-space indentation automatically when you press the Tab key. When errors persist in Mu:
- Click the Check button in the top menu. Mu will highlight whitespace inconsistencies and point directly to the line containing mixed tabs or non-standard spaces.
- Select all text (
Ctrl+AorCmd+A) and press Tab followed by Shift+Tab to force Mu to re-format the indentation hierarchy into standard spaces.
Configuring VS Code
For Senior Phase and FET learners working in Visual Studio Code, open Settings (Ctrl+,) and search for Render Whitespace. Set the value to all or boundary. Additionally, search for Insert Spaces and ensure it is checked so that the Tab key always writes spaces.
A Reliable Classroom Protocol
When an entire lab of learners is working through robotics or automation challenges, a teacher cannot spend twenty minutes manually inspecting invisible tabs on thirty different screens. Teaching learners this three-step troubleshooting protocol solves the vast majority of indentation faults:
| Step | Action | Mechanism |
|---|---|---|
| 1. Reveal | Turn on whitespace rendering | Exposes mixed dots (spaces) and arrows (tabs) instantly. |
| 2. Strip | Backspace to column zero | Clears out hidden non-breaking spaces or mismatched bytes completely. |
| 3. Re-indent | Press Tab once per level | Allows the editor's space-conversion rule to insert clean ASCII spaces. |
For schools rolling out coding and physical computing, structured curriculum resources and vetted browser-based development environments—such as the workflows we deploy through Sheen Infinity for schools—help bypass these low-level editor discrepancies entirely by automating indentation hygiene from day one.
If a script refuses to run despite looking visually aligned, trust Python's error over your eyes: strip the indentation down to the left margin and re-indent using purely configured space characters.



