Lesson 3 of 6 · Getting Started · 2 min read · Updated
When Python complains: reading error messages
An error message is Python telling you what went wrong. Learn to read the last line and fix the mistake.
Understand
Sometimes Python cannot run your code. It stops and shows an error message.
An error is not a failure. It is Python telling you what went wrong, so you can fix it.
Note
Every programmer sees errors every day. Reading them is a skill, and you can learn it now.
Use it
Here is a line with a mistake. Python will run it and show an error.
print(Welcome)
NameError: name 'Welcome' is not defined
Read the last line first. It has two parts: the kind of error, and what went wrong. Here Python says it does not know the name Welcome.
What to do when you see an error
- Read the last line of the message.
- Look at the line of your code that it points to.
- Fix one thing, then run the code again.
Practice
This code gives an error. Run it, read the last line, and fix it so it prints Hello.
To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.
Expected output
Hello
Need a hint?
- Python says it does not know a name. Check the capital letter.
- Write
print("Hello")with a small p.
Show a solution
print("Hello")
This code gives a different error. Run it, read the message, and fix it so it prints Hello.
To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.
Expected output
Hello
Need a hint?
- Count the brackets. Every opening bracket needs a closing one.
- Write
print("Hello").
Show a solution
print("Hello")
Python says it does not know the name Welcome. Fix the code so it prints the word Welcome.
To try it, copy the code into a file on your computer and run it. Your output should match the expected output below.
Expected output
Welcome
Need a hint?
- Text needs quotes around it.
- Write
print("Welcome").
Show a solution
print("Welcome")
Which part of an error message do you read first?
Why it works
Python lists the steps that led to the error, and the last line says what went wrong. This is why you read from the bottom.
Two errors you will meet often
NameError: Python does not know a name. Often the spelling or the capital letter is wrong, or quotes are missing.SyntaxError: the code is not written in a way Python understands, for example a missing bracket.
The error message also names the line of your code where Python stopped. The mistake is usually on that line or just before it.
Create a free account to save your progress, or sign in.