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.
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
SyntaxErrorand aNameError - 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
- 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.
- 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.
- 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:
print("Line one works")
pront("Line two has a typo")
It prints this, then stops with a NameError:
Line one works
| Line | What it does |
|---|---|
| 1 | This line is fine, so it runs and prints Line one works. |
| 2 | pront 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:
- Traceback (most recent call last) – just a heading meaning "here's where things went wrong". You can skip it.
- line 2 – where Python noticed the problem. Look here first.
- NameError – the kind of error. The name tells you what sort of mistake to look for.
- 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?
print("Legs on 3 spiders:")
print(3 + 8)
It prints:
Legs on 3 spiders: 11
| Line | What it does |
|---|---|
| 1 | Prints the label. No problem here. |
| 2 | Prints 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
- Read the message. Find the line number and the error type.
- Look at that line – and the line above. Check brackets, quote marks, spelling and capitals.
- Change one thing at a time. If you change five things at once, you won't know which one fixed it.
- Run it again. A new error is often good news: it means you fixed the first one and Python got further.
- 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✓ Done
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✓ Done
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✓ Done
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✓ Done
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✓ Done
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
| Wrong | Right | Why |
|---|---|---|
Only looking at the line in the message | Look at that line and the line above | A missing bracket or quote mark is often reported one line late. |
Changing lots of things at once | Change one thing, then run | Small steps show you exactly what fixed the problem. |
Thinking "no error" means "correct" | Check the output against what you expected | Logic errors don't give any message. |
Giving up at the first error | Read it, fix it, run again | Every 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
- A bug is a mistake in a program; debugging is finding and fixing it. Errors are normal.
- An error message gives a line number, an error type and details.
- SyntaxError: the code breaks Python's writing rules. Check the line and the one above.
- NameError: Python doesn't know a name – check spelling, capitals and quote marks.
- Logic errors give no message, so always check your output.
- Debug in small steps: read, look, change one thing, run again.
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: England national curriculum, computing KS3: use a textual programming language; design, write and debug 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