A fun guide to finding, fixing, and mastering programming errors with Trace Tables!
Before we start hunting down issues in our code, let's learn the developer lingo:
An error, flaw, or mistake in a computer program that prevents it from working correctly.
The step-by-step process of finding, tracking down, and fixing bugs in your source code.
Mental check or paper-and-pencil test of an algorithm's steps before actually running it on a computer.
Not all programming errors are the same! Let's explore the three troublemakers you will meet:
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"
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
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!
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.
Click "Step Code" to trace this simple Python loop that sums numbers from 1 to 3!
| Line Number | i | total | Output |
|---|---|---|---|
| - | - | - | - |