INFS1201 Computer Programming

Lesson 2: Debugging Techniques • Mohammed Shifan

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.

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!

How Programs Are Really Built:
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

  1. Read the last line first: The error name and exact reason.
  2. Look at the caret (^): Marks the exact character where Python gave up reading.
  3. Look at the code line: Python displays the exact line it could not understand.
  4. Look at the file and line number: Where Python noticed the failure. (Note: Unclosed brackets are named where they opened!).
⚠️ Danger: Not one single line of a program runs if there is a SyntaxError. Even a 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
Reading a Traceback Bottom-Up:
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!
Fix: Use 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:

  1. Where was it supposed to be set?
  2. How was it computed?
  3. 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.

Score: 0 / 7

Practice Challenges & Solutions

🎓

Enter Student Name

Please enter your full name as you would like it to appear on your Certificate.

INFS1201 Computer Programming • Lesson 2 Assessment

Certificate of Completion

presented to

Student Name

for successfully mastering Lesson 2 with a score of

0 / 7

Date
Mohammed Shifan
Awarded by