Use an error message to fix a program
Quick review (2 minutes)
Imagine a note tells you to fetch a box labelled “Ticket”.
Start at the reported line or the spot IDLE marks. The mark gives you an approximate place to look. The fix may be nearby. For the misspelled name, compare the name inside the parentheses with the one beside =. For the unclosed call, look for the missing ). Make one change and run the file again so you can see what that change did.
In IDLE for Python 3.13, use File, then New File to open an Editor window, where you type and save source. Save the file under the example’s name. In the Editor’s Run menu, choose Run Module. Read the program’s output in the Shell window. For these examples, type the source in the Editor and leave the Shell for reading.
For the misspelled name, find the file name and line number in the Shell report. Open that file in the Editor if needed. Go to the indicated line, counting from 1 and including blank lines. Read the error class on the report’s last line. For the unclosed call, look for the spot IDLE marks in the Editor during the syntax check.
Make one change in the Editor and save the file.
Choose Run Module again and compare what appears in the Shell with your predicted output.
Check the whole name when the error says NameError. If a call has an open parenthesis, find its closing partner before running the file. An unclosed call can stop the whole file during parsing.
1 In plain words
Imagine a note tells you to fetch a box labelled “Ticket”. The label on the box says “Tickets”. You can compare the two words, add the missing s to the note and try again.
The instructions you type for Python are called source. When you save your source in the Editor, you can give the file a name such as seats.py. If a message names seats.py and says line 2, read it as “look at the second line of seats.py”. The error class names the kind of problem. Remember where to look first, then read the class for a clue about what to change.
In seats = 3, = gives the name seats to the value 3. The marks ( and ) are parentheses. Writing print(seats) is a call. Python looks up the value of seats and passes it to print. The text the program prints is its output. A reply you type when a running program asks for one is supplied input.
2 Picture it
The file seats.py has print(seat) on line 2. The Shell reports NameError. Change seat to seats and run again to see 3.
3 Why
When line 1 gives 3 the name seats, line 2 has to use that same spelling to get the value by name. Python recognizes print(seat) as a call and hits an error when it looks for seat. The traceback is the part of the report that points to where the error happened. For this NameError, the class appears at the start of the last line of the report.
Syntax is the set of rules for how to write Python instructions, such as closing the parentheses in a print call. An unclosed call at the end of a file gives a SyntaxError before the file runs. When you choose Run Module, IDLE checks the file’s syntax before running it.
4 The rule
Start at the reported line or the spot IDLE marks.
The mark gives you an approximate place to look. The fix may be nearby. For the misspelled name, compare the name inside the parentheses with the one beside =. For the unclosed call, look for the missing ). Make one change and run the file again so you can see what that change did.
5 How
- In IDLE for Python 3.13, use File, then New File to open an Editor window, where you type and save source. Save the file under the example’s name. In the Editor’s Run menu, choose Run Module. Read the program’s output in the Shell window. For these examples, type the source in the Editor and leave the Shell for reading.
- For the misspelled name, find the file name and line number in the Shell report. Open that file in the Editor if needed. Go to the indicated line, counting from 1 and including blank lines. Read the error class on the report’s last line. For the unclosed call, look for the spot IDLE marks in the Editor during the syntax check.
- Make one change in the Editor and save the file.
- Choose Run Module again and compare what appears in the Shell with your predicted output.
6 Worked example
Find the missing letter
seats = 3
print(seat)
Standard input
Empty
Standard output
Empty
Exception NameError
Read the error class on the last line and use the file and line number to find the misspelled name.
Run the repaired file
seats = 3
print(seats)
Standard input
Empty
Standard output
3
Write seats next to 3 on paper. The second line looks up that name and prints the value.
Read a syntax error
print("Gate open")
print("Gate closed"
Standard input
Empty
Standard output
Empty
Exception SyntaxError
The file has an unclosed call. Python reports a syntax error during parsing, before either line prints.
Close the parentheses
print("Gate open")
print("Gate closed")
Standard input
Empty
Standard output
Gate open
Gate closed
Save the corrected file. Read the two lines in order to predict its output.
7 The trap
Check the whole name when the error says NameError. If a call has an open parenthesis, find its closing partner before running the file. An unclosed call can stop the whole file during parsing.
Quick checks
Quick check 1 of 2
Read this new file or type it in the Editor and save it as badges.py.
badges = 7
print(bagde)Supplied input
None. What error class do you expect, what one change would you make, and what output do you expect in the Shell after running the repaired file?
Show the steps and the answer
Expect NameError. Change line 2 to print(badges) in the Editor.
Expected output after rerunning
7Why: Python looks for bagde when it reaches line 2. Line 1 gave 7 the name badges.
Source: Python Software Foundation, 8. Errors and Exceptions
Quick check 2 of 2
Read this new file or type it in the Editor and save it as numbers.py.
print(5)
print(8Supplied input
None. What error class do you expect, and what output will the file produce on that run?
Show the steps and the answer
Expect SyntaxError. The file produces no program output.
Why: The second call needs a closing ). The syntax check stops the file before either line runs.
Source: Python Software Foundation, IDLE — Python editor and shell
Sources
- 8. Errors and Exceptions. Python Software Foundation. Python Software Foundation License Version 2. Read 25 Nov 2026.
- IDLE — Python editor and shell. Python Software Foundation. Python Software Foundation License Version 2. Read 25 Nov 2026.