When something goes wrong · Python Programming | StemPy Lab
Course 1.1·Lesson 4 of 5
When something goes wrong
Python is strict about spelling and punctuation. When something is wrong, it stops and shows an error message with the line number, the kind of error, and often a hint. Typical mistakes are a capital letter in print, a missing quotation mark, and a missing bracket. Read the message from the last line up, find the line, fix one thing, and run again. Fix one error at a time, because a later error may be only a result of an earlier one.
about 35 min1 simulation2 practice questions
Lesson 4 of 5Show all lessonsHide
Try the code and the simulations beside the lesson.
What you will be able to do
After this lesson you can
read an error message and find the line and the kind of error,
name the three most common beginner mistakes,
follow a short routine to fix any program, and
feel calm when a program does not work.
Everyone makes mistakes
Every programmer makes mistakes, every day, also the best ones. When Python cannot follow an instruction, it stops and shows an error message. This is a help, not a punishment. It tells you the line number and what is wrong. Programmers do not try to write programs without mistakes. They learn to read the messages and to fix the mistakes fast.
An error message in a pink box with three lines: Traceback, File main.py line 1, and NameError: name Print is not defined, Did you mean print. Three notes point to it. Which line? Look at the line number. What kind of mistake? A name that Python does not know. The hint: Python guesses what you meant. The title says to read it from the bottom up.
Read an error message from the bottom. The last line says the kind of error and what is wrong. The lines above it say where.
Four common mistakes
Run each program, and read its message.
Mistake 1: a wrong spelling.
Mistake 2: a missing quotation mark.
Mistake 3: a missing bracket.
Mistake 4: something is missing in a sum.
The two kinds of message
A SyntaxError means that the line is not written in the way that Python expects. Python could not even start the program.
A NameError, a TypeError and others mean that the program started, but a line failed when it ran. The lines before it were done.
A routine to fix any program
When a program does not work, do not panic and do not change ten things at once. Use these steps.
Read the last line of the message. What kind of error is it?
Look at the line number. Go to that line. Sometimes the mistake is on the line above, for example a missing bracket.
Look for the usual things: a capital letter, a missing quotation mark, a missing bracket, a missing colon, a misspelt name.
Fix one thing, and run again.
Read the new message. Fixing one error may show another one that was hidden behind it. That is normal, and it means that you are going forward.
Each of these three mistakes is a NameError: Python meets a name that it does not know. A missing quotation mark or bracket is different. It stops the program before any line runs.
Common mistakes about mistakes
Reading the message from the top. Start at the bottom.
Changing many things together. Then you do not know which change helped.
Being afraid of the red text. It is a helper that tells you where to look.
Typing in a word processor. It changes the quotation marks to curly ones, and Python rejects them.
Practice
Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
Warm-up. Open the lab Fix the three bugs. The program has three mistakes. Fix them one at a time, and watch the message change.
Build. Open the problem Find the three bugs, and write down the kind of error for each one.
Stretch. Take any program from the earlier lessons, and add one mistake on purpose. Give it to a friend, and ask them to find it from the message only.
Remember these
An error message tells you the kind of error, and the line. Read it from the bottom.
The three most common mistakes are a wrong spelling or capital letter, a missing quotation mark, and a missing bracket.
Fix one thing at a time, and run again.
A SyntaxError means that Python cannot read the line. The other errors happen when a line runs.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
What does NameError: name 'Print' is not defined tell you?
Why is print("Hello) an error?
A message points at line 4, but line 4 looks fine. Where else can the mistake be?
Why do we fix one error at a time?
Enroll free in Say Hello to Python
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.