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.
Work through it, step by step
Work through it free and interactively, with each step checked before the next.
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
Put the steps of debugging a program into a sensible order.
- Run the program and notice it gives the wrong output or crashes
- Work out the exact input that causes the problem
- Use a trace table to find the line where things go wrong
- Work out what is actually wrong on that line
- Fix it, then re-test to check the bug is really gone
Diagnose it
Three programs, three bugs. Name each one.
- A program will not run at all, and the editor highlights a line with an unmatched bracket.
- A working-looking program crashes only when the user enters 0, because it then divides by 0.
- You have found a bug on a line. What is the safest next step?
Syntax, logic, runtime
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.
Three errors, one hunt
Explain the three kinds of programming error - syntax, logic and runtime - and describe how you would find and fix a logic error.
- Explain what a syntax error is, and give an example
- Explain what a logic error is, and why it is the hardest to spot
- Explain what a runtime error is, and give an example
- Describe how testing and a trace table would help you find a logic error