Unit 8 • Lesson 9

Debugging Logical and Runtime Errors

Overview

Not all bugs cause crashes — some just produce wrong results. You'll learn how to trace logic flaws, check assumptions, and systematically test different inputs to isolate the issue, developing advanced debugging skills for complex problems.

Intermediate 25–30 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • Logical errors: Bugs where code runs but produces wrong results.
  • Runtime errors: Errors that occur during execution.
  • Debugging strategies: Techniques for finding these errors.
  • Tracing logic: Following code execution step by step.
  • Testing approaches: Using tests to find logical errors.

Logical vs Runtime Errors

Understanding the difference helps you debug more effectively:

Runtime Errors

Code crashes with an error message. Python tells you exactly what went wrong and where.

Logical Errors

Code runs without crashing, but produces incorrect results. These are harder to find.

Example: Logical Error
# This code runs but gives wrong answer
def calculate_discount(price, discount_percent):
    # Bug: should multiply by discount_percent / 100
    return price * discount_percent  # Wrong!

# Test case
result = calculate_discount(100, 20)  # Should be 20, but returns 2000!
print(result)  # Output: 2000 (wrong!)

Debugging Logical Errors

Logical errors require systematic debugging:

1

Identify Expected vs Actual

Know what the correct result should be. Compare it to what you're getting.

2

Trace Through Code

Follow the code step-by-step with your test input. See where logic goes wrong.

3

Check Assumptions

Verify your assumptions about how the code works. Are they correct?

4

Test Edge Cases

Try different inputs: empty lists, zero values, negative numbers, etc.

5

Use Print/Debugger

Add print statements or use a debugger to see variable values at each step.

Common Logical Error Patterns

Watch out for these common mistakes:

Off-by-One Errors

Using wrong index or loop range

Wrong Operator

Using + instead of *, or == instead of !=

Missing Edge Cases

Not handling empty lists, zero, or None

Wrong Order

Operations in wrong sequence

Type Confusion

Treating strings as numbers or vice versa

Incorrect Logic

Wrong conditional logic or algorithm

Summary

In this lesson, you learned:

  • Logical errors: Code runs but produces wrong results
  • Runtime errors: Code crashes with error messages
  • Debugging: Trace through code, check assumptions, test edge cases
  • Common patterns: Off-by-one, wrong operators, missing edge cases
  • Tools: Print statements and debuggers help find logical errors

Remember

Logical errors are trickier than runtime errors because they don't announce themselves. Systematic debugging—tracing through code, testing edge cases, and checking assumptions—is key to finding them!

End-of-Lesson Exercises

Think about these questions to reinforce what you've learned:

Exercise 1: Error Types

What's the difference between logical errors and runtime errors? Which is harder to find?

Exercise 2: Debugging Strategies

What strategies help you find logical errors? How do you trace through code?