Common Python Errors and How to Fix Them
When an error appears, inspect not only the words and symbols on a line, but also the whitespace that positions the line.
When Correct-Looking Code Fails
A Python program can look nearly correct and still fail because the problem is not always visible in the characters you normally notice. Syntax errors are usually easy to find, but whitespace errors are a significant gotcha: spaces and tabs are invisible, so readers tend to overlook them.
When an error appears, inspect not only the words and symbols on a line, but also the whitespace that positions the line.
Reading the Visible and Invisible Parts
A syntax error concerns the form of the Python text. These errors are usually easy to find because the problem is often associated with a visible part of the text. Whitespace errors require more care because the relevant characters may be spaces or tabs, which are invisible during ordinary reading.
The diagram separates what your eyes see from what the text contains. Two lines may appear to have the intended relationship while containing different kinds of invisible whitespace. A practical inspection should therefore make whitespace visible or otherwise verify that the lines use a consistent pattern.
A Reliable Error-Checking Sequence
- 1Begin with the location associated with the reported problem rather than scanning the entire program at once.
- 2Check the visible syntax around that location for an obvious mismatch or omission.
- 3If the visible text looks correct, inspect the whitespace on the affected lines.
- 4Compare nearby lines and look for a difference between spaces and tabs.
- 5Make the smallest clear correction, then check the same area again.
Diagnosing a Line That Looks Correct
A learner sees a syntax-related error near a group of lines. The visible words appear correct, but the whitespace is not easy to inspect.
- 1Start with the reported area
Limit the first inspection to the line associated with the problem and the nearby lines that visually belong with it.
- 2Inspect visible syntax
Look for a problem in the visible characters first, because syntax errors are usually easy to find.
- 3Inspect whitespace
If the visible characters do not explain the problem, check for invisible spaces or tabs. This is the important gotcha highlighted by the source.
- 4Normalize the pattern
Use one clearly consistent whitespace pattern for the affected lines, then check the area again.
The diagnostic process moves from visible syntax to invisible whitespace instead of assuming that an apparently aligned line is actually formatted consistently.
Mistakes That Hide the Cause
Common mistake Looking only at visible characters
What it looks likeThe words and symbols appear correct, so the learner stops checking.
Why it failsWhitespace errors can be difficult to notice because spaces and tabs are invisible.
Do this insteadInspect the whitespace whenever the visible syntax does not explain the problem.
Common mistake Assuming apparent alignment proves consistent formatting
What it looks likeTwo lines look equally indented on the screen.
Why it failsThe source specifically identifies spaces and tabs as a gotcha because they are easy to ignore.
Do this insteadVerify the actual whitespace pattern instead of relying only on visual alignment.
Common mistake Searching through the whole program immediately
What it looks likeThe learner changes unrelated lines before examining the area associated with the error.
Why it failsA broad search makes it harder to isolate whether the issue is visible syntax or whitespace.
Do this insteadStart near the reported problem, then widen the inspection only if necessary.
Practice the Diagnostic Habit
Imagine that a Python error appears near a group of lines whose visible text looks correct. Describe the order in which you would investigate the problem. Your answer should distinguish between checking visible syntax and checking invisible whitespace.
Hints
- Begin with the area associated with the error.
- Check visible syntax before moving to the less visible cause.
- Remember that spaces and tabs can be difficult to notice.
What do you think happens?
If the visible characters on a line look correct, what should you inspect next when diagnosing a possible whitespace error?
Reveal answer
The invisible spaces and tabs on the affected lines
Whitespace errors are tricky precisely because spaces and tabs are invisible and easy to ignore.
Key Takeaways
- 1Syntax errors are usually easier to locate because their visible form often points toward the problem.
- 2Whitespace errors are harder to diagnose because spaces and tabs are invisible.
- 3When visible syntax looks correct, inspect the whitespace on the affected and nearby lines.
- 4A focused sequence of checking the reported area, visible syntax, and then whitespace helps prevent random changes.
At a glance
- Syntax errors are usually easy to find, but they still have occasional gotchas.
- Spaces and tabs are invisible, which makes whitespace errors especially easy to overlook.
- When the visible text does not explain the problem, inspect the whitespace directly.
- Diagnose locally first: examine the affected area, check visible syntax, and then verify whitespace consistency.