Skip to content

Finding and Fixing Mistakes in Python

Programming Python Difficulty 1/5

Beginning of the lesson

Finding and Fixing Mistakes in Python

How to read error messages and fix common beginner bugs

Big Idea: Every programmer makes mistakes — even experts! When your code has a mistake, Python does not get angry. Instead it prints a helpful message called an error that tells you where the problem is and what went wrong. Learning to read that message is a superpower.
By the end you will be able to:
  • stay calm when you see a red error message,
  • read a Python traceback to find the line number and the kind of error,
  • recognise common beginner errors like NameError, TypeError, and SyntaxError,
  • use print() to peek inside your program and find the bug.

1. A bug is not a disaster

A mistake in code is called a bug. Fixing bugs is called debugging. Bugs are completely normal — they are part of coding, like erasing and re-writing is part of drawing.

Analogy: Think of Python as a very careful friend reading your recipe out loud. If a step does not make sense, your friend stops and points at the exact line: "Here — I do not understand this part." Python does the same thing with an error message. It is trying to help you, not scold you.

Do not fear the red text. Read it slowly. It usually tells you almost exactly what to fix.

2. How to read a traceback

When Python hits a problem it cannot continue past, it stops and prints a traceback. Look at this tiny program that has a typo:

message = "Hello"
print(mesage)

When you run it, Python prints:

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    print(mesage)
NameError: name 'mesage' is not defined

Do not panic — read it from the bottom up. The most useful parts are:

  • The last line tells you the type of error and a short message. Here it is NameError: name 'mesage' is not defined.
  • The line 2 tells you exactly where to look. The bug is on line 2.

Now the fix is easy: we wrote mesage but the box is called message. Fix the spelling:

message = "Hello"
print(message)

Expected output:

Hello
Traceback (most recent call last): File "main.py", line 2, in <module> print(mesage) NameError: name 'mesage' is not defined WHERE: line number WHAT: error type + message
Read a traceback: the line number says WHERE, the last line says WHAT went wrong.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers