Learning LibraryCore Coding LibraryKids

Errors and debugging: fixing the glitch

Every coder makes mistakes — even the best. A bug is just a mistake in your code, and debugging is the fun detective work of finding and fixing it.

The big idea

When code breaks, Python shows a red error message that tells you what went wrong and which line to check.

See it in code

1Start simple

The friendliest bug-finding trick is print() — it shows you what a value really is. Print a value, change it, and print it again to check your work:

python
lives = 3
print("lives:", lives)
lives = lives - 1
print("lives:", lives)
Run it — the print shows the value before and after:
lives: 3
lives: 2

Now you can see that lives really dropped from 3 to 2. When code misbehaves, a print like this is how you catch it.

2A step further

Prints are most useful inside a loop, where a value changes each time around. Here we print lives every pass, so we can watch the countdown happen:

python
lives = 3
while lives > 0:
    print("You have", lives, "lives left")
    lives = lives - 1

print("Game over!")
Run it — watch the value on every loop:
You have 3 lives left
You have 2 lives left
You have 1 lives left
Game over!

If the countdown ever skipped or got stuck, the printed lines would show the exact pass where it went wrong. That's the heart of debugging.

3In our world

Same trick on a running total. We add up points and print the score after each one — so if it ever looks wrong, we can see exactly where:

python
score = 0
for points in [10, 5, 20]:
    score = score + points
    print("score is now", score)

print("Final score:", score)
Run it — you can watch the score climb, step by step:
score is now 10
score is now 15
score is now 35
Final score: 35

Seeing the score after each step means that if it ever climbed wrong, you'd spot the exact moment — just like the countdown. That is print-debugging: your first and friendliest bug-finder.

The same idea, everywhere

Bugs happen in every kind of program — games, art, apps. The steps are always the same: read the red message, look at the line number it points to, and add prints to watch what's really happening. Then fix it and run again.

Try it yourself

Make a bug on purpose: change score + points to score + point (missing an s). Run it, read the red NameError, and see how it names the wrong word. Then fix it back.

The common mistake

Panicking at the red text and ignoring it. The error message is helping you — it names the problem and the line. Read it slowly: the last line usually says exactly what went wrong.

What it unlocks

Debugging leans on print output to watch values, and gets easier once you know your data types.

Older, or want more depth? Read the Teens version →