Python · Beginner level

Beginner level · Module 1: Python Setup and First Programs · Lesson 3 of 3

Running Scripts and Reading Error Messages

Understand syntax errors and tracebacks so that mistakes become quick fixes instead of dead ends.

In this lesson you will

  • Identify the line and type of an error from a traceback
  • Fix common beginner errors (indentation, names, types)

Key concepts

SyntaxErrorNameErrorTypeErrortraceback

The lesson

Errors are normal: professional programmers see them every day. What separates beginners from confident programmers is reading the error message calmly instead of guessing. Python's message (the traceback) tells you the file, the line number, the type of error and a short description.

Read a traceback from the bottom up. The last line names the error: SyntaxError means Python could not understand the code's shape (a missing bracket or colon); NameError means you used a name that does not exist yet, often a typo; TypeError means you combined values that do not fit, such as text plus a number; ValueError means the type was right but the value was not, such as int('twelve').

Fix one error at a time and run again. Changing several things at once makes it hard to know which change helped.

Step by step

  1. Run the broken script and copy the last line of the error.
  2. Find the line number in the traceback and open that line.
  3. Decide which error type it is and what that type usually means.
  4. Fix only that problem, save, and run again.
  5. Record the error and your fix in a two-column table.
Troubleshooting tree

What is this Python error telling me?

A troubleshooting path from the last line of a traceback to the most likely fix.

Does the last line say SyntaxError or IndentationError?
  • Yes
    Python could not read the code's shape. Check the line shown and the line above it for a missing colon, bracket or quote, or inconsistent indentation.
  • No
    Does it say NameError?
    • Yes
      A name is used before it exists. Look for a spelling or capitalisation difference, or a variable defined later or inside a function.
    • No
      Does it say TypeError or ValueError?
      • Yes
        A value has the wrong type or content — often text from input() used as a number. Convert with int() or float() and validate the input.
      • No
        Search the exact error name in the Python documentation, then reproduce the problem with the smallest possible example and ask for help with that.

Notes

Always read the traceback from the bottom up: the last line names the error, and the lines above show where it happened.

Practice activity

Fix a provided script that contains five deliberate errors, writing down the error name and the fix for each.

Expected result: You can name each error type and line number and correct all five errors.

Check your understanding

  1. In a traceback, where is the actual error message?

    Show answer

    On the last line, after the error type name.

  2. What causes a NameError?

    Show answer

    Using a name (variable or function) that has not been defined, often a spelling mistake.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.