Getting started with Python
Read and fix basic errors
Use a SyntaxError and its line number to find and fix a missing quotation mark.
Lesson 6 of 63
Reading: about 3 min
Lesson + practice: about 12 min
Difficulty: Beginner
On this page5 sections
An error message explains why Python could not continue. Its last line names the error type, and the output usually shows a file and line number. Use those clues to inspect the indicated line and nearby code.
Try a missing quotation mark
Temporarily replace the contents of hello.py with the two-line broken example below. Save and run it. The error should name line 2 and end with SyntaxError because that line has no closing quotation mark. Neither line prints: Python checks the whole file for syntax errors before executing it.
print("The first line is valid.")
print("Hello, Python!) File "hello.py", line 2
print("Hello, Python!)
^
SyntaxError: unterminated string literal (detected at line 2)Your terminal may show the full path to hello.py instead of just its name, and recent Python versions colour the output. The last line is what matters: a SyntaxError reported on line 2.
Go to line 2, add the missing quotation mark after the exclamation mark, save, and rerun the file. Both lines should now print.
print("The first line is valid.")
print("Hello, Python!")The first line is valid. Hello, Python!
Chapter project: your introduction script
Replace the temporary error exercise with your final hello.py script. Include a comment and at least three print() lines. Introduce yourself, say that you are learning Python, and describe one task you would like to do with it. Use your own words.
# My first Python program
print("Hi, I am Sam.")
print("I am learning Python.")
print("I want to automate a repetitive task.")Hi, I am Sam. I am learning Python. I want to automate a repetitive task.
Why this works
The filename and line number point to where Python detected a problem. The cause can be on that line or immediately before it, so inspect nearby punctuation too. A SyntaxError prevents the whole file from starting; after correcting it, rerun the script to check the result.
Completion check
Run your final hello.py script and check that all three messages appear. You are ready to continue when you can create and save a .py file, run it from a terminal, change its output, and use an error message to find and fix a simple mistake.
Where should you look first when Python reports an error?
Reveal answer
Read the last line of the error, then inspect the indicated line in your file.
Try another case
If a message points at line 3 but line 3 looks correct, where else should you inspect?
Check your answer
Reveal answer
Inspect line 3 and the lines immediately before it for an unclosed quote, bracket, or other syntax mistake.
Your progress
Reveal the check answer or reach the end to complete this lesson.