Be a detective
Imagine a shop app that charges 1000 pounds for a sandwich. The customer is angry. The shop owner is worried. Somebody must find the cause.
Even good programmers write bugs. What makes them good is how fast they find and fix them. They work like a detective: look at the clues, make a guess, test it.
Key words
| Word | What it means | Example |
|---|---|---|
| Debug | To find and fix mistakes in code. | |
| Bug | A mistake in a program. | |
| Locate | To find where in the code the mistake is. | |
| Traceback | The message Python shows when an error stops the program. It names the line. | |
| Print statement | A print line added to see the value of a variable. | |
| Debugger | A tool that runs your code step by step. | |
| Breakpoint | A marker where the debugger pauses the program. | |
| Variable | A name that holds a value. | |
| Trace table | A table where you write the value of each variable after each step. | |
| Robust | Keeps working, or stops safely, when something unexpected happens. | |
| Syntax error | The code breaks the rules of Python. The program does not start. | a missing colon |
| Runtime error | The program starts, then crashes while it runs. | dividing by zero |
| Logic error | The program runs but gives a wrong answer. There is no message. | a total that is 1 too big |
The cells students fill in are left blank in this preview.
Run it: read the message
total = 10
bonus = 5
print("Pay is", totl + bonus)
# Run it. Python stops and shows a message.
# Which line number does it give? What is the name of the error?
Read the message from the bottom
The message looks scary, but it is a gift. It tells you three things: where, what and why.
Start at the last line. It names the kind of error and says what is wrong. Then look up for the line number.
Parts of a Python error message
| Part | Example | What it tells you |
|---|---|---|
| Line number | line 3 | Where to look first |
| The code line | print("Pay is", totl + bonus) | The line that failed |
| Kind of error | NameError | What type of problem it is |
| Explanation | name 'totl' is not defined | Why it failed: a spelling mistake |
The cells students fill in are left blank in this preview.
Debugging has two jobs
Locate the error: find where it is. Correct the error: fix it.
Do the jobs in order. Never change code at random. Make one change, then run the program again. If you change five things at once you will not know which one helped.
Put the debugging steps in order
Have a go
Put the debugging steps in order
3 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
Three kinds of error
| Kind | What happens | Example |
|---|---|---|
| Syntax error | The code breaks the rules of Python. The program does not start. | A missing colon after if |
| Runtime error | The program starts, then crashes while it runs. | Dividing by zero |
| Logic error | The program runs but gives a wrong answer. There is no message. | Adding 1 too many to a count |
The cells students fill in are left blank in this preview.
Quick check: which kind of error?
Have a go
Quick check: which kind of error?
3 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.