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
- Run the broken script and copy the last line of the error.
- Find the line number in the traceback and open that line.
- Decide which error type it is and what that type usually means.
- Fix only that problem, save, and run again.
- Record the error and your fix in a two-column table.
What is this Python error telling me?
A troubleshooting path from the last line of a traceback to the most likely fix.
- YesPython 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.
- NoDoes it say NameError?
- YesA name is used before it exists. Look for a spelling or capitalisation difference, or a variable defined later or inside a function.
- NoDoes it say TypeError or ValueError?
- YesA value has the wrong type or content — often text from input() used as a number. Convert with int() or float() and validate the input.
- NoSearch the exact error name in the Python documentation, then reproduce the problem with the smallest possible example and ask for help with that.
- Yes
- Yes
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
-
In a traceback, where is the actual error message?
Show answer
On the last line, after the error type name.
-
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.