▶ TrainingGet Pro
All tools & lessonsAll toolsAllGet the appApp
Learning departments Music, Cookery, Programming and Art are open – explore →

Python for complete beginners · Unit 1: Getting started · Lesson 3 of 24

Lesson 3: Errors are normal – reading error messages and debugging

Every programmer sees error messages every day. Learn to read them, fix the most common ones, and debug like a professional.

⏱ About 35 minutesFor complete beginners: grades 6–8 and adultsRuns in your browser – nothing to install

0 of 24 lessons finished

By the end of this lesson you will be able to

  • explain what a bug is and what debugging means
  • read a Python error message and find the line it points to
  • fix a SyntaxError and a NameError
  • spot a logic error, where the program runs but gives the wrong answer
  • use a simple step-by-step way to debug

Everyone gets errors

If you have tried the exercises so far, you have probably already seen Python complain about something. Good! Learning to deal with errors is one of the most valuable skills in programming, and today you'll learn how.

A mistake in a program is called a bug. Finding and fixing bugs is called debugging. There's a famous story from 1947: engineers working on an early computer at Harvard found a real moth stuck inside the machine, and taped it into their logbook as the "first actual case of bug being found". People already called faults "bugs" before then, but the story stuck – and so did the moth, which is now kept in a museum.

Remember from Lesson 1: computers are very literal. One missing bracket is enough to stop a program. That isn't because you're bad at this. It's because the computer can't guess what you meant.

Three kinds of error

  1. Syntax errors. The code breaks the rules of how Python must be written (its syntax) – like a missing bracket or quote mark. Python can't even start, so nothing runs.
  2. Errors while running. The code is written correctly, but Python hits a problem part way through, such as a name it doesn't know. It stops at that line. Anything printed before it still appears.
  3. Logic errors. The program runs with no error message at all, but does the wrong thing – like adding when it should multiply. These are the sneakiest, because Python can't spot them. Only you can.

How to read an error message

When Python stops, it shows an error message. It can look scary at first, but it's just Python telling you three useful things. Here's a program with a spelling mistake on line 2, and the message Python gives:

Worked example: a misspelt instruction
print("Line one works")
pront("Line two has a typo")

It prints this, then stops with a NameError:

Line one works
Line by line
LineWhat it does
1This line is fine, so it runs and prints Line one works.
2pront should be print. Python has never heard of anything called pront, so it stops here.

Python's full message is called a traceback. It looks like this:

Reading an error message Traceback (most recent call last): File "<stdin>", line 2, in <module>NameError: name 'pront' isn't defined ① a heading② where: line 2 ③ what kind of error④ the details
An error message with four parts labelled: the Traceback heading, the line number (line 2), the error type (NameError), and the details (name 'pront' isn't defined).
  1. Traceback (most recent call last) – just a heading meaning "here's where things went wrong". You can skip it.
  2. line 2 – where Python noticed the problem. Look here first.
  3. NameError – the kind of error. The name tells you what sort of mistake to look for.
  4. name 'pront' isn't defined – the details, in Python's words. (Some versions of Python say is not defined; it means the same.)

In our editor, you'll see Python's own words as well as a plain-English explanation underneath the output. Try it – run this, read the message, then fix the typo and run it again:

Try it When it runs with no error, you've debugged your first program.

print("Line one works")
pront("Line two has a typo")

Output

The errors you'll meet most

SyntaxError: Python can't understand the line

A SyntaxError means the code isn't written the way Python needs. The usual causes are a missing bracket, a missing quote mark, or curly “smart quotes” pasted from a word processor.

Watch out: when a bracket is missing, Python often reports the next line, because it only realises something is wrong when it reaches the next line. So always check the line it names and the line above.

Try it Python points at line 2, but the missing bracket is at the end of line 1. Add it and run again.

print("Hello"
print("Goodbye")

Output

NameError: Python doesn't know that name

A NameError means you used a word Python doesn't recognise. Usually it's a spelling mistake (pirnt), a capital letter (Print), or text where you forgot the quote marks: print(Hello) makes Python look for something called Hello.

IndentationError: unexpected spaces at the start

The spaces at the start of a line are called indentation, and in Python they matter. For now, every line should start right at the left edge. A stray space at the start of a line gives an error. (In the editor here, Python may call this a SyntaxError.) You'll use indentation on purpose in Unit 3.

TypeError: things that don't go together

A TypeError means you tried to do something with the wrong kind of value – like joining text and a number with +, which you met in Lesson 2.

Logic errors: no message, wrong answer

This program is meant to show how many legs 3 spiders have. Spiders have 8 legs. It runs without any error. Is it right?

Worked example: a logic error
print("Legs on 3 spiders:")
print(3 + 8)

It prints:

Legs on 3 spiders:
11
Line by line
LineWhat it does
1Prints the label. No problem here.
2Prints 11. But 3 spiders × 8 legs = 24! The programmer typed + where they needed *. Python did exactly what it was told.

The only way to catch a logic error is to check the output. Before you run a program, work out roughly what it should print. Then compare.

A simple way to debug

The debugging loop 1. Read the message2. Look (and above)3. Change one thing4. Run again Repeat until it works
A debugging loop: read the message, look at the line and the line above, change one thing, run again – and repeat until it works.
  1. Read the message. Find the line number and the error type.
  2. Look at that line – and the line above. Check brackets, quote marks, spelling and capitals.
  3. Change one thing at a time. If you change five things at once, you won't know which one fixed it.
  4. Run it again. A new error is often good news: it means you fixed the first one and Python got further.
  5. Still stuck? Explain your code out loud, line by line, to someone – or to a toy. Programmers call this rubber duck debugging, and it works surprisingly well.

Your turn: be a bug hunter

Each program below has at least one bug. Fix it, press Run to test, then press Check.

Exercise 1 of 5 Bug 1: The missing bracket Fix or change the code

Fix this program so it prints:
Good morning!
Time for school.
The exercise opens here when you scroll to it. It needs JavaScript switched on.

Stuck? Get a nudge

Mr Babbage: Count the brackets on line 1.

Still stuck? Get a hint

Mr Babbage: Line 1 opens a bracket but never closes it. Add ) at the end of line 1.

Show me the answer

Try typing it yourself rather than copying – it sticks better.

print("Good morning!")
print("Time for school.")

Exercise 2 of 5 Bug 2: Two bad names Fix or change the code

This program should print Ready, Steady and Go! on three lines. It has two bugs. Run it, read each error, and fix them one at a time.
The exercise opens here when you scroll to it. It needs JavaScript switched on.

Stuck? Get a nudge

Mr Babbage: Fix the first error, run again, then fix the next one.

Still stuck? Get a hint

Mr Babbage: Line 1 has a capital P. Line 3 has two letters swapped round.

Show me the answer

Try typing it yourself rather than copying – it sticks better.

print("Ready")
print("Steady")
print("Go!")

Exercise 3 of 5 Question: What kind of error? Choose an answer

A program is meant to print the number of legs on 3 spiders (24). It runs with no error message, but prints 11. What kind of error is this?
The exercise opens here when you scroll to it. It needs JavaScript switched on.

Stuck? Get a nudge

Mr Babbage: Python showed no error message at all.

Still stuck? Get a hint

Mr Babbage: Syntax errors and NameErrors always come with a message. The other kind doesn't.

Show me the answer

Try typing it yourself rather than copying – it sticks better.

A logic error – It runs without a message, so it isn't a SyntaxError or NameError. But the answer is wrong, so it's a logic error: the program did exactly what it was told, and what it was told was wrong.

Exercise 4 of 5 Bug 3: The wrong answer Fix or change the code

This program should show how many legs 5 spiders have (each spider has 8 legs). It runs, but the answer is wrong. Fix the logic error.
The exercise opens here when you scroll to it. It needs JavaScript switched on.

Stuck? Get a nudge

Mr Babbage: The bug is one symbol on line 2.

Still stuck? Get a hint

Mr Babbage: 5 lots of 8 is a multiplication. In Python, multiply is *.

Show me the answer

Try typing it yourself rather than copying – it sticks better.

print("Legs on 5 spiders:")
print(5 * 8)

Exercise 5 of 5 Bug 4: Missing quote marks Fix or change the code

Fix this program so it prints:
Welcome
to the bug hunt!
The exercise opens here when you scroll to it. It needs JavaScript switched on.

Stuck? Get a nudge

Mr Babbage: Read the error: it's a NameError on line 1.

Still stuck? Get a hint

Mr Babbage: Put quote marks round Welcome so Python knows it's a string.

Show me the answer

Try typing it yourself rather than copying – it sticks better.

print("Welcome")
print("to the bug hunt!")

Common mistakes when debugging

WrongRightWhy
Only looking at the line in the messageLook at that line and the line aboveA missing bracket or quote mark is often reported one line late.
Changing lots of things at onceChange one thing, then runSmall steps show you exactly what fixed the problem.
Thinking "no error" means "correct"Check the output against what you expectedLogic errors don't give any message.
Giving up at the first errorRead it, fix it, run againEvery programmer fixes errors all day long. It gets quicker with practice.

Quick quiz

5 questions. Pick an answer for each one – if it's wrong, have another go. Finish them all to complete the lesson.

The quiz opens here when you scroll to it. It needs JavaScript switched on.

Recap

That's the end of Unit 1 – well done! In Unit 2 you'll meet variables: a way for your programs to remember things.

Key words from this lesson

Tap or hover over a word for its meaning, or open the full glossary of code.

Finished?

Complete the quiz (60% or more) and this lesson is ticked off on your progress bar automatically – or tick it yourself.

About this lesson

How it's marked: exercises and quizzes are marked by Classfern in the browser – a practice check, not an exam. Your code and progress stay in this browser; we never see them.

Coach tips: Mr Babbage and Miss Lovelace pop up with tips on your first visit. Untick Coach tips at the top of the lesson (or press "All tips off" on a tip) to switch them off. Show me how always works.

Curriculum: CSTA K–12 CS Standards (grades 6–8): 2-AP-11 (variables and data), 2-AP-17 (testing and debugging), 2-AP-19 (documenting programs).

Python is a registered trademark of the Python Software Foundation. Classfern is independent and is not affiliated with or endorsed by it.

Python course home · Glossary of code · Programming Department