Lesson 2: Debugging Techniques — Finding & Fixing the Three Kinds of Error
Why every programmer debugs, the beginner myth, syntax/runtime/logic errors, tracebacks, and using print() and type() as tools.
Save or Print Notes
Export this study guide as a clean PDF for exam preparation and quick revision.
1. The Debugging Mindset & The Beginner Myth
✕ The Beginner Myth
"I should write the whole program first, and then run it. If I plan it carefully enough, it will work the first time."
Result: Many bugs tangled together across 100 lines. Hours of frustrating guesswork.
✓ The Professional Reality
"I run it after every few lines. The longer I wait to test, the harder every bug is to find."
Result: One bug on its own, isolated to the 5 lines you just typed. Fixed in seconds!
Write a little → Run it → Read message → Fix one thing → Run it again (repeat until it works).
2. The Three Types of Error
Before you fix anything, identify which of the three error types occurred:
| Error Type | Does Program Start? | Error Message? | Who Notices It? | Typical Example |
|---|---|---|---|---|
| Syntax Error | No | Yes | Python, before line 1 runs | SyntaxError: expected ':' (colon missing) |
| Runtime Error | Yes | Yes | Python, part way through execution | ZeroDivisionError: division by zero |
| Logic Error | Yes | No (Silent!) | You or your end user | Average outputs 25.0 instead of 85.0 |
3. Syntax Errors & How to Read Them
Common Syntax Errors
| What You Typed | Error Raised |
|---|---|
while count < 5 |
SyntaxError: expected ':' |
name = "Fatima |
SyntaxError: unterminated string literal |
total += int(input("Val: ") |
SyntaxError: '(' was never closed |
whille count < 5: |
SyntaxError: invalid syntax |
| Indented unexpectedly | IndentationError: unexpected indent |
How to Read a SyntaxError
- Read the last line first: The error name and exact reason.
- Look at the caret (
^): Marks the exact character where Python gave up reading. - Look at the code line: Python displays the exact line it could not understand.
- Look at the file and line number: Where Python noticed the failure. (Note: Unclosed brackets are named where they opened!).
print("Start") on line 1 will never execute!
4. Runtime Errors & Tracebacks
A runtime error occurs when Python understands your code and begins executing it, but encounters an illegal operation part way through.
| Runtime Error | Typical Cause | Message Python Prints |
|---|---|---|
ZeroDivisionError |
Dividing by zero: total / count when count is 0 |
division by zero |
ValueError |
Invalid conversion: int("three") or int("3.5") |
invalid literal for int() with base 10 |
TypeError |
Mismatched types: "20" + 5 or input() >= 50 |
can only concatenate str (not "int") to str |
NameError |
Typo in variable name: totl += score |
name 'totl' is not defined. Did you mean: 'total'? |
IndexError |
Accessing past the end: name[5] for "Ahmed" |
string index out of range |
Start at the very bottom line (what went wrong). Step up one line to see the code line that crashed. Continue upward through caller frames to trace where the call originated.
5. Logic Errors: When Nothing Complains
The Accumulator Trap
Logic errors break no rules of Python. The code starts, runs to completion, and outputs a number—but the number is wrong!
total = 0
count = 0
while count < 3:
score = float(input("Score: "))
total = score # ❌ BUG: Overwrites instead of adding!
count += 1
print("Average:", total / 3)
# If scores are 88, 92, 75: prints 25.0 instead of 85.0!
total += score (same as total = total + score). An accumulator must always include its own prior value!
print() and type() as Detectives
When a calculated number looks wrong, work backwards:
- Where was it supposed to be set?
- How was it computed?
- What value did I expect there?
# Insert temporary debug prints:
print("total =", total, type(total))
print("count =", count, type(count))
Temporary debug prints turn guesses into concrete evidence. Delete them once the bug is resolved.