Reading error messages · Python from Zero
What do you do when Python says something is wrong?
Course contentsWhy you need it
Every programmer sees errors every day; they are not a sign you are bad at this. An error message is a clue that tells you where and what. Learning to read it calmly saves more time than any other skill.
The intuition
An error is like a GPS saying "wrong turn at the 3rd street". Three things to read:
- The line number: where Python noticed the problem (in this site, that line turns red).
- The type: what kind of problem.
- The message: details.
The most common types:
| Type | Meaning |
|---|---|
| SyntaxError | The grammar is wrong: a missing bracket, quote or colon. Nothing runs at all. |
| NameError | You used a name Python doesn't know: a typo, or text without quotes. |
| TypeError | You combined values that don't fit, such as text + number. |
A special trap for Chinese keyboards: symbols like ( ) “ ” : , look almost like ( ) " " : , but Python can't read them. Switch to English input.
Definition
Syntax error
A syntax error means the code breaks the grammar rules of the language, so the program cannot start.
Runtime error
A runtime error happens while the program is running; the lines before it have already run.
Examples
A typo in a name
Try it yourselfRun it: the error points at line 2. Fix the capital N and run again.
name = "Ada"
print(Name)This example stops with an error on purpose: reading the message is the point.
- L1Line 1 works: it stores "Ada" under the name
name. - L2
Name(capital N) is a different name. Python has never seen it: NameError.
Chinese punctuation
Try it yourselfLook closely at the brackets. Replace them with English ones and run again.
print("first line")
print("second line")This example stops with an error on purpose: reading the message is the point.
- L2( and ) are full-width (Chinese) brackets. Python stops before running anything, even line 1.
Common mistakes
The real mistake can be on the line before
A missing ) on line 3 is often reported on line 4, where Python finally gets confused. Check the line shown and the one above.
Fixing one error reveals the next
Python reports one error at a time. After a fix, run again; a new error usually means progress.
Quick quiz
1. What is printed?
print("one")
print("two"
print("three")Show the answer
Answer: C
A syntax error stops the program before it starts, so not even line 1 runs.
2. What is printed before the error?
print("start")
print(10 / 0)
print("end")Show the answer
Answer: A
This is a runtime error: line 1 already ran, line 2 fails, line 3 never runs.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictWhat gets printed first?
This program crashes. Type what it prints before the error.
- ModifyFix two bugs
This program should print
Total: 5and thenDone. Fix the bugs. - WriteRewrite it correctly
Someone typed this on a Chinese keyboard and it has three problems:
print("Python") print("Year:", 2026) Print("OK")Write a correct version in the editor (type it yourself, don't paste). It should print:
Python Year: 2026 OK
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Syntax error | 语法错误 | An error where code breaks the language's grammar, so the program cannot start. |
| Runtime error | 运行时错误 | An error that happens while the program is running. |
| NameError | 名字错误 | An error raised when code uses a name that has not been defined. |
| TypeError | 类型错误 | An error raised when an operation is used on a value of the wrong type. |