Bug Hunt
Every coder makes bugs - the skill is finding them. Learn to tell a syntax error from a logic error from a runtime error, and how testing and trace tables track a bug down.
Revise this, the fun way
Play it interactively, earn XP and build a streak, free.
Start revising freeWhat you'll cover
Every coder makes bugs 🐛
No programmer writes perfect code first time. The real skill is **debugging** - finding and fixing the mistakes. And to fix a bug fast, you first work out **which kind** it is. There are three: **syntax** errors, **logic** errors and **runtime** errors. Each looks different, and each is hunted differently.
Three kinds of bug 🗂️
The three error types, plus the two tools you use to find them.
It will not even run 🚫
A Python program has a missing colon after an if statement, so it refuses to run and reports a SyntaxError. What kind of error is this?
- A syntax error
- A logic error
- A runtime error
- No error at all
Match each error to its telltale sign 🔗
- Syntax error
- Logic error
- Runtime error
- Testing
- The program will not run at all
- The program runs, but the answer is wrong
- The program runs, then crashes partway through
- Running the program with data to check the output
Logic vs runtime ⚖️
The two trickiest bugs both let the program **start** running. The difference is what happens next.
It runs, but... 🤔
A program calculates an average, runs with no error message, but always prints a number that is too small. What kind of error is this?
- A logic error
- A syntax error
- A runtime error
- A hardware fault
How to hunt a bug 🔎
Two tools find bugs fast. **Testing**: run the program with chosen **test data** and compare the actual output to what you expected. Good test data is a mix of **normal**, **boundary** (edge) and **erroneous** (invalid) values. **Trace tables**: write out each variable and follow its value **line by line** as the program runs. The moment a variable holds a value you did not expect, you have found the faulty line.
Spot the syntax errors ✅
Select the TWO problems that are SYNTAX errors - ones that stop the program running at all.
- A missing closing bracket
- The keyword written as pirnt instead of print
- Using minus where you meant plus
- Dividing a number by zero
Tidy code, fewer bugs 🧹
Bugs hide in messy code. A few habits make them far easier to spot and to avoid. **Meaningful names** (total, not t) say what a variable is for. **Comments** explain the tricky bits. **Indentation** and **white space** show the structure at a glance. None of this changes what the program does - but it makes a hidden bug stand out, and helps the next person understand your code.
It crashes halfway 💥
A program runs fine until it reaches the line int('hello'), where it stops with an exception. What kind of error is this?
- A runtime error
- A syntax error
- A logic error
- A spelling mistake in a comment
The debugging loop 🪜
An interactive activity.
Diagnose it 🧭
An interactive activity.
The summary 📝
A _____ error breaks the language's rules, so the program will not run at all. A _____ error lets the program run but gives the wrong result, with no message. A _____ error lets it run and then crashes partway through. To find bugs you use _____ - comparing output to what you expected - and a _____ table to follow each variable line by line.
Your turn ✍️
An interactive activity.