My first Python program was six lines long, and it had a bug. I stared at it for an hour. The error said “NameError: name ‘total’ is not defined”, and I was sure the computer was wrong — total was right there, on line three. It was not. I had typed “totl” on line three and “total” on line five — and Python, being very literal, treated them as two different things. That hour taught me the one skill every programmer uses daily: debugging — finding the bug, understanding it, fixing it. Let’s learn it step by step, with a real example.

📅 Want a study plan for this topic?

Get a free 7-day learning plan in your calendar — one small lesson every day.

What Is Debugging, Really?

Debugging is detective work for your code. A bug is any mistake that makes your program crash, give a wrong answer, or behave strangely — and every programmer writes bugs daily, beginners and Google engineers alike. There are three kinds you will meet. A syntax error stops your program before it runs — a missing colon, a forgotten bracket. A runtime error crashes it mid-way — dividing by zero, opening a file that is not there. And a logic error is the sneaky one: the program runs fine and gives you a confidently wrong answer. Python helps you with the first two — it prints a traceback, pointing at the exact line where things went wrong. The third kind is where debugging earns its name — only you can spot that the answer is wrong. When I first saw a traceback, I thought the computer was shouting at me. It was handing me a map. In my opinion, reading error messages is the fastest way to go from "stuck for an hour" to "fixed in five minutes".

The Main Idea

Most beginners get stuck right here: they change five things at once, run the code, and pray. That is guessing, not debugging. The main idea is the opposite — change one thing, test it, watch what happens. The computer does exactly what you wrote, never what you meant, so the job is to find the gap, one small check at a time.

In Simple Words

Here is the whole method, in five steps:

  1. Reproduce it — run the program and make the bug happen again, on purpose.
  2. Read the error — the last line of the traceback names the problem; the line above names where.
  3. Find the line — open that exact line and read it slowly, out loud if it helps.
  4. Print the values — add print() before the suspicious line and look at what the variables really hold.
  5. Fix one thing, test again — change a single line, run, and check. Repeat until the answer is right.

Let's Solve an Example: The Average That Was Wrong

Meet Bilal. He wrote a tiny program to average five exam marks: 78, 85, 92, 81, and 89. The right answer is 85 — 425 divided by 5. But his program printed 17.8. Let's debug it together:

marks = [78, 85, 92, 81, 89]
total = 0
for mark in marks:
    total = mark      # BUG: overwrites instead of adding!
average = total / len(marks)
print(average)        # prints 17.8
StepWhat We CheckWhat We See
1. Read the errorAny traceback?No crash — so it is a logic error
2. Print totalprint(total) after the looptotal is 89, not 425
3. Look at the looptotal = markEach round replaces total instead of adding
4. Fix one linetotal += markNow total grows: 78, 163, 255, 336, 425
5. Test againRun the programaverage = 425 / 5 = 85. Correct!

The bug was one missing character: = instead of +=. Each round threw the old total away and stored only the latest mark, so total ended as 89, and 89 divided by 5 gave 17.8. One print() found it in seconds. That is the whole secret: when the answer looks wrong, print the variable right before the line that uses it — nine times out of ten, the value is not what you assumed. I recommend the print() habit before anything fancy — it works in every editor, needs no setup, and most working programmers still debug small programs this way. If the error is a crash, not a wrong answer, my try-except guide shows how to handle it.

Diagram: the five debugging steps - reproduce, read the error, find the line, print the values, fix one thing and test again

Animated: Bilal's average program step by step - the buggy total, the print check, the one-line fix, and the correct answer 85

print() vs Debugger: Which Tool, When?

ToolWhat It DoesBest ForCost
print()Shows a variable's value at a pointQuick checks, small programsFree, built in
TracebackPoints at the exact failing lineCrashes and runtime errorsFree, automatic
VS Code debuggerPause, step line by line, watch valuesBig programs, confusing bugsFree, needs setup
Rubber duckExplain the code out loudLogic errors your eyes skipFree, slightly silly

A debugger lets you pause time: set a breakpoint, run, and watch each variable change as you step through — powerful, and worth learning once print() stops being enough. But honestly, in your first year of Python, print() plus the traceback solves nearly everything. The same patience helps with functions — test each small one on its own before trusting the whole program.

3 Mistakes Every Beginner Makes

I made all three, so you do not have to. First, changing five things at once: if the program suddenly works, you have no idea which change fixed it. One change, one test, every time. Second, ignoring the error message: the traceback gives you the file, the line, and the problem — read the last line first, it is the summary. Third, debugging in your head, not on the screen: "I think total is 425" is a guess — print(total) is a fact. And remember the if-else rule too: when a condition behaves oddly, print the condition itself before blaming the code around it.

Test Your Understanding

Quiz: Bilal's program printed 17.8 instead of 85. What was the bug?

  1. The list of marks was wrong.
  2. total = mark overwrote the total instead of adding to it.
  3. len(marks) counted 6 items instead of 5.
  4. The print statement was in the wrong place.

Answer: B. Each loop round replaced total with the latest mark, so total ended as 89 — and 89 / 5 = 17.8.

Conclusion

Debugging is not a talent; it is a habit: reproduce, read, print, fix one thing, test again. Bilal's whole mystery was one missing + sign — and one print() found it in seconds. So next time your program gives a weird answer, do not rewrite everything. Add a print(), look at the real numbers, and let the computer tell you what it is doing — that is the moment you stop guessing and start debugging.

📅 Want a study plan for this topic?

Get a free 7-day learning plan in your calendar — one small lesson every day.

Related Articles

Written by Muhammad Junaid Niazi

Founder of The Geeks Guru — writing simple, practical tech and programming tutorials from Layyah, Pakistan.

Never miss a new article

Subscribe to The Geeks Guru newsletter and get every new article in your inbox.

Loading