Everyone Can Learn Program
中文 Start learning
Course 2 · Python BasicsLesson 2 of 14Free to read

Reading error messages · Python from Zero

What do you do when Python says something is wrong?

Course contents

Why 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:

  1. The line number: where Python noticed the problem (in this site, that line turns red).
  2. The type: what kind of problem.
  3. The message: details.

The most common types:

TypeMeaning
SyntaxErrorThe grammar is wrong: a missing bracket, quote or colon. Nothing runs at all.
NameErrorYou used a name Python doesn't know: a typo, or text without quotes.
TypeErrorYou 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 yourself

Run 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.

Step by step
  1. L1Line 1 works: it stores "Ada" under the name name.
  2. L2Name (capital N) is a different name. Python has never seen it: NameError.

Chinese punctuation

Try it yourself

Look 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.

Step by step
  1. 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")
  1. A
    one
  2. B
    one
    two
  3. C
    (nothing is printed)
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")
  1. A
    start
  2. B
    start
    end
  3. C
    (nothing is printed)
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.

  1. PredictWhat gets printed first?

    This program crashes. Type what it prints before the error.

  2. ModifyFix two bugs

    This program should print Total: 5 and then Done. Fix the bugs.

  3. 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

TermChineseDefinition
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.