๐Ÿ•ต๏ธโ€โ™‚๏ธ Debugging ๐Ÿž and Trace Tables

A fun guide to finding, fixing, and mastering programming errors with Trace Tables!

Key Terms to Remember ๐Ÿง 

Before we start hunting down issues in our code, let's learn the developer lingo:

๐Ÿ›

The "Bug"

An error, flaw, or mistake in a computer program that prevents it from working correctly.

๐Ÿงน

Debugging

The step-by-step process of finding, tracking down, and fixing bugs in your source code.

๐Ÿ•ต๏ธโ€โ™€๏ธ

Dry Run

Mental check or paper-and-pencil test of an algorithm's steps before actually running it on a computer.

The 3 Major Types of Errors โš ๏ธ

Not all programming errors are the same! Let's explore the three troublemakers you will meet:

โŒ 1. Syntax Error Grammar Mistake

What is it? This happens when you break the rules of the programming language. The computer simply doesn't understand your instructions, so the program refuses to start!

# Python Example with Syntax Error:
prnt("Hello World") # Uh oh! It should be "print"
๐Ÿค” 2. Logic Error Thinking Mistake

What is it? The program runs perfectly without crashing, but it gives you the wrong answer because your formula or thinking was incorrect.

# Python Example with Logic Error (Goal: Average of two numbers):
average = num1 + num2 / 2 # Oops! Bidmas rules mean division happens first. Should be (num1 + num2) / 2
๐Ÿ’ฅ 3. Runtime Error Crash Mistake

What is it? The syntax is perfect, the code starts running, but then it encounters an impossible action and crashes while running (e.g., dividing by zero or looking for a file that isn't there).

# Python Example with Runtime Error:
result = 10 / 0 # Error! You can't divide a number by zero!

What is a Trace Table? ๐Ÿ“Š

A Trace Table is a powerful tool used by programmers to track the values of variables as they change, line by line, through a dry run. It helps us pinpoint exactly where a logic error has sneaked into our loops or calculations.

๐Ÿ’ก How to use it: Write down each variable name as a column header. As you mentally execute each line of code, record the new values underneath them!

Interactive Trace Table Simulator ๐ŸŽฎ

Click "Step Code" to trace this simple Python loop that sums numbers from 1 to 3!

1: total = 0
2: for i in range(1, 4):
    3: total = total + i
4: print(total)
Line Number i total Output
- - - -