Learning outcomes
By the end of this lesson you should be able to: tell the three kinds of error apart — syntax, logic and semantic; trace a short loop by hand to predict its output; read an error message and use it to point you at the cause; and apply the four habits of debugging: read, run, ruminate, retreat.
Prerequisites
Lessons 4 and 5 (Conditions and Loops). You need to be able to read an if/elif chain and a loop, because the whole point of this lesson is to predict what such code does — and to find the mistake when it does not.
The big idea
The best way to understand debugging is to understand what went wrong in the first place. Almost every bug is one of three kinds. A syntax error is a grammar mistake — the program cannot even be read. A logic error is a mistake in the order of steps — the program runs, but the steps are in the wrong sequence. A semantic error is a mistake in meaning — the steps are correct and in order, but they say something other than what you intended. Knowing which kind you are looking at tells you where to look.
Why this matters before the technique
The three error types are different enough that trying to fix them all the same way wastes time. A syntax error is the easiest. Python cannot understand the line at all, so it points at the exact spot — often the fix is a missing colon or a misplaced quote. The clue is that the program does not produce output; it refuses to run.
A logic error is subtler because the program runs fine. The steps are all valid Python; they are simply in the wrong order. Remember the example of the water bottle: "take a drink, put it in your backpack, walk to the library, then put the top back on." Every step is a sensible action, but the order makes the whole thing wrong. This kind of error gives no error message — the program runs and produces the wrong answer, which is why it is the hardest to notice.
A semantic error is the cruelest. Every step is valid and in the right order, but a step says the wrong thing. You meant minutes but wrote seconds; you meant greater-than but wrote less-than. The program does exactly what you told it — the bug is that you told it the wrong thing. This is why debugging starts with going back to the problem itself, not just to the syntax.
Worked example 1: tracing a loop by hand
The habit that prevents the most logic bugs is tracing: walking through a loop line by line, keeping a running note of each variable, to predict what it prints before you run it.
total = 0
for i in range(1, 4):
total = total + i
print("total =", total)total = 6Read it one pass at a time. The loop visits i equal to 1, then 2, then 3 — remember range(1, 4) stops before 4. Each pass adds the current i onto the running total. First pass: 0 + 1 = 1. Second: 1 + 2 = 3. Third: 3 + 3 = 6. So total is 6 when the loop ends. Tracing like this, on paper, is the single best hedge against logic bugs: it forces you to predict the output, and if your prediction differs from what the program prints, you have found a bug before it ships.
Worked example 2: a syntax error and its message
Dropping the colon on a for line produces a syntax error — the program will not run at all. Learn to read the message as Python pointing at the spot it got confused.
# This is a syntax error — the colon is missing.
for i in range(3)
print(i) File "<stdin>", line 2
for i in range(3)
^
SyntaxError: expected ':'The message tells you two things: the line number and the character it was confused about. Here it points right after the closing bracket, which is exactly where the colon should be. The instinct to fear error messages is worth unlearning. They are not judgments; they are Python saying, as politely as it can, "I got as far as this and then I lost my place." Most syntax errors have one of a handful of causes: a missing colon, a missing or mismatched quote, a mismatched parenthesis, or a misspelt keyword.
Worked example 3: a logic error with no message
Here the program runs without complaint but does the wrong thing. The cause is ordering — the bottleneck of a logic bug: steps that are individually fine but in the wrong sequence.
# Bug: the drink is taken before the lid is opened.
step = "put the lid on"
print("step 1:", "take a drink")
print("step 2:", step)step 1: take a drink
step 2: put the lid onThis is why logic errors are the dangerous ones. There is no traceback, no red text, no clue at all except that the output is wrong. The only way to catch it is to know what the output should be. That is why tracing and predicting are such central skills: if you have trained yourself to predict the output first, you will notice the moment the program disagrees with you.
Worked example 4: a semantic error — the wrong meaning
Here the code is valid and in a sensible order, but a value is recorded in the wrong unit. The program does exactly what you wrote — you just told it the wrong thing.
# Bug: recorded seconds, but meant minutes.
duration_seconds = 120
print("Duration in minutes:", duration_seconds // 60)Duration in minutes: 2Notice the code is clean and the answer is technically right for 120 seconds. Two minutes is the correct conversion of 120 seconds. The bug is that the input was a number of minutes, so dividing by 60 was the wrong operation entirely. A semantic error cannot be found by looking at the code alone — you must look at the problem, decide what the answer should be, and compare. This is why the best debuggers spend time at the start making sure they understood the question, not just the syntax.
Common mistakes
Watch outRandom-walk programming. Making random small changes hoping one fixes the bug can take forever. Before you change anything, have a hypothesis about which line is wrong and why.
Watch outAssuming an error message is a judgement. It is not. It is a location and a clue. Read the line number and the caret first, then decide — the fix may even be a line earlier than where Python noticed.
Watch outOnly reading, or only running, or only thinking. Each alone fails differently. Reading catches typos, running catches order, thinking catches meaning — you need all three.
Watch outRefusing to retreat. The best move, when a program is too tangled, is to undo your recent changes back to a version that worked. Copy it aside if you are squeamish, but get back to a known-good state.
Check your understanding
A program runs but prints the wrong answer, with no error message. Which error type is this, and where do you look?
What is the first thing to read in a Python error message?
Why does tracing a loop by hand help prevent logic errors?
Multiple choice
Which error is a mistake in the grammar of the code, so Python cannot even run it?
A program runs cleanly but the steps are in the wrong order. Which error is this?
Which error is when the code is correct and ordered well, but does not do what you intended?
Which habit is NOT part of effective debugging?
Why is tracing a loop useful before running it?
Where does a missing colon most likely show up?
What is the first step when a program is too tangled to debug?
You recorded a value in seconds but meant minutes. What kind of error is this, and what does it require to catch?
Summary and next
Bugs are one of three kinds: syntax (cannot be read), logic (wrong order), and semantic (wrong meaning). Reading, running, ruminating and retreating are the four debugging habits. Error messages are coordinates, not judgements; tracing gives you a predicted output to compare against; and the hardest errors are found by going back to the problem, not by staring at the code.
Next: functions. So far every program has been a single list of steps. The next lesson introduces the reuse pattern — write a set of instructions once, give it a name, and call it wherever it is needed — which is the whole idea of composing programs from pieces, and the topic you will keep returning to.