Skip to content

10 Debugging and Validation

Debugging means finding and fixing problems. Validation means checking that the result makes sense.

Run in Binder Open in Colab Download notebook

Read here. Practice in a notebook.

This page shows the original notebook. Open Binder or Colab to run and edit cells. Work one cell at a time: some examples are deliberately broken for debugging practice. Download your work before leaving Binder; its sessions are temporary.

View original source

Learning Objectives

By the end, you should be able to:

  • Recognize syntax errors, runtime errors, and logic errors.
  • Read an error message without panic.
  • Use a simple debugging process.
  • Validate output by checking it against an expected result.

Concept Introduction (no code first)

Errors are a normal part of programming. Debugging is not guessing; it is a step-by-step process for finding what changed or what does not match.

Simple process: run the code, read the error or output, identify the problem, fix one issue, and run again.

Syntax Errors

A syntax error means Python cannot understand the code as written. This is often caused by a missing quote, parenthesis, or colon.

Think Before You Run: Will this code run? What small typing mistake do you see?

# Intentional SyntaxError: the closing quote is missing.
# Run this once, read the message, then fix it.
print("Disk check complete)

Reading Error Messages

When an error occurs, focus on the last line of the message, the error type, and the line number mentioned.

Simple reading plan:

  1. Find the last line.
  2. Find the error type.
  3. Find the line number or highlighted line.
  4. Look for a small mismatch, missing symbol, or wrong variable name.

Runtime Errors

A runtime error happens while the program is running. Two common examples are NameError and TypeError.

Think Before You Run: What type of error might occur if a variable was never created?

# Intentional NameError: login_count was not created.
print("Login count:", login_count)

Think Before You Run: What type of error might occur when text and a number are mixed?

# Intentional TypeError: this tries to add text and a number.
attempts_message = "Login attempts: " + 3
print(attempts_message)

Logic Errors

A logic error means the code runs, but the answer is wrong. Logic errors are harder to find because the code runs but gives the wrong result.

Think Before You Run: Does this result make sense? The average of 80, 90, and 100 should be 90.

# Logic error: this runs, but the first average is wrong.
score_1 = 80
score_2 = 90
score_3 = 100

wrong_average = score_1 + score_2 + score_3 / 3
correct_average = (score_1 + score_2 + score_3) / 3

print("Wrong average:", wrong_average)
print("Correct average:", correct_average)

Validation and Testing

Validation means checking whether the program gives the correct result. Testing means trying a few values to see if the program still behaves correctly.

# Validation example: login attempts should be in a safe range.
attempts = 4

if attempts <= 5:
    print("Login attempts are in range")
else:
    print("Too many login attempts")

# Expected result for attempts = 4: in range

Expected vs actual is a simple validation habit.

# Simple validation example
expected_total = 12
actual_total = 5 + 7

print("Expected:", expected_total)
print("Actual:", actual_total)

Think Before You Run

The disk usage is 92. Which message should appear?

# Test a disk usage threshold.
disk_usage = 92

if disk_usage > 90:
    print("Critical disk warning")
else:
    print("Disk usage is okay")

Real-World Examples

Debugging and validation help with login attempts, disk usage calculations, and system checks.

# Example 1: login attempts
login_attempts = 6
print("Too many attempts:", login_attempts > 5)

# Example 2: disk usage calculation
used_space = 80
total_space = 100
disk_percent = used_space / total_space * 100
print("Disk usage percent:", disk_percent)

# Example 3: system check
system_online = True
print("System check passed:", system_online)

Debugging Process

Use this simple process:

  1. Run the code.
  2. Read the error or output.
  3. Identify the problem.
  4. Fix one issue.
  5. Run again.

Guided Debugging Practice

Steps:

  1. Run the code.
  2. Read the error.
  3. Fix one issue.
  4. Run again.
# TODO: Fix the variable name so both lines match.
disk_usage = 75
print("Disk usage:", disk_usag)
# TODO: Fix the calculation so the average is correct.
reading_1 = 60
reading_2 = 70
reading_3 = 80

average_reading = reading_1 + reading_2 + reading_3 / 3
print("Average reading:", average_reading)

# Expected result: 70

Common Mistakes

Most errors come from small mistakes. Fix them one at a time.

Quick reminders:

  • Check for missing quotes or symbols.
  • Check for undefined variables.
  • Check for mixing strings and numbers.
  • Check for incorrect calculations.
  • Test with a value where you know the expected output.

Parsons Problem

The lines below are scrambled. Put them in the correct order in the next cell.

print("Disk usage percent:", disk_percent)
disk_percent = used_space / total_space * 100
used_space = 75
total_space = 100
# TODO: Reorder the corrected debugging-related code.
# Create values first, calculate, then print.

used_space = 75
total_space = 100
disk_percent = used_space / total_space * 100
print("Disk usage percent:", disk_percent)

Mini Lab

Fix the small broken program and verify that the output matches the expected result.

# Mini Lab: fix and verify
# Step 1: Run the broken program.
# Step 2: Fix the comparison.
# Step 3: Validate the output against the expected result.
# Goal: show whether a user should get an access warning.
# Expected result with login_attempts = 6: Access warning needed: True

login_attempts = 6
warning_limit = 5

# TODO: Fix this line so the result matches the expected output.
access_warning = login_attempts < warning_limit

print("Access warning needed:", access_warning)

Wrap-Up

Debugging is a normal part of programming. Testing improves program reliability.

Next, you will explore Python tools and applications.

Reflection

What part of debugging feels most challenging right now, and what step could help you feel more confident?