livepython.net
CHAPTER 02The Print Function
2.5  COMMON ERRORS

Common print() Errors in Python and How to Fix Them

Missing Quotes

The most common print() error of all: forgetting the quotes around text.

PYTHONedit me
print(Hello)
OUTPUT
NameError: name 'Hello' is not defined

Why it happens

Without quotes, Python doesn't see text. It sees a name, and it looks for something called Hello. Nothing has that name, so Python stops with a NameError: "I don't know this name".

You'll learn in the Variables chapter that names can hold values. That's exactly why Python looks for one here.

Two words without quotes

With more than one word, the message changes:

PYTHONedit me
print(Hello World)
OUTPUT
SyntaxError: invalid syntax. Perhaps you forgot a comma?

Now Python sees two names side by side, with nothing between them, and it can't make sense of the line at all. That's a SyntaxError: the code breaks Python's grammar rules. Python even guesses what you meant ("Perhaps you forgot a comma?"), but here the real fix is quotes.

The fix

Put the text inside quotes, single or double:

PYTHONedit me
print("Hello World")
OUTPUT
Hello World

Rule of thumb: if a NameError or SyntaxError points at a print() line, first check that every piece of text has quotes at both ends.

NameError on a print() line? Look for text without quotes.

your turnSpot the bug

Which line breaks?

One line stops this program. Tap it.

Missing or Extra Brackets

Every opening bracket ( needs exactly one closing bracket ). Too few or too many, and Python stops before running anything.

A missing bracket

PYTHONedit me
print("Good morning"
OUTPUT
SyntaxError: '(' was never closed

The message says it clearly: a ( was opened and never closed. Add ) at the end, and it works.

An extra bracket

PYTHONedit me
print("Good morning"))
OUTPUT
SyntaxError: unmatched ')'

"Unmatched" means this ) has no ( to pair with. Remove one.

Brackets inside brackets

Calculations can have their own brackets, so lines can have several pairs:

PYTHONedit me
print("Total:", (5 + 3) * 2)
OUTPUT
Total: 16

A good habit: count them. Every ( you open, close it with a ), in the right place. Many code editors help by colouring matching pairs or highlighting the partner bracket when your cursor is next to one.

Why the error can point at the wrong line

A missing ) sometimes makes Python report the error on the next line, because Python keeps reading, hoping to find the ). If an error points at a line that looks perfect, check the line before it too.

Count your brackets: every ( needs its ).

your turnFix the code

Make it print: Total: 8

It should print: Total: 8

print("Total:", (5 + 3)
This code does not work yet. Fix it, then press Run.

Capital Letters and Wrong Quotes

Python is very strict about how you write things. Two small slips cause a lot of errors.

Capital letters matter

Python is case-sensitive: it treats capital and small letters as different. print is a function. Print and PRINT are just unknown names:

PYTHONedit me
Print("Hello")
OUTPUT
NameError: name 'Print' is not defined

Phones often add a capital letter at the start of a line. If you type code on a phone, watch out for that.

Quotes that don't match

A string must start and end with the same kind of quote:

PYTHONedit me
print("Hello')
OUTPUT
SyntaxError: unterminated string literal (detected at line 1)

It opens with " and closes with ', so Python never finds the end of the text. "Unterminated" means "never ended".

Smart quotes from copy and paste

Word processors and chat apps change straight quotes " " into curly "smart" quotes “ ”. They look nice, but Python doesn't accept them:

PYTHONedit me
print(“Hello”)
OUTPUT
SyntaxError: invalid character '“' (U+201C)

If you copy code from a document or a chat message, and get an invalid character error, retype the quotes yourself.

print in small letters, and the same straight quote at both ends.

your turnSpot the bug

Which line breaks?

One line stops this program. Tap it.
your turnFix the code

Make it print: Good night

It should print: Good night

Print("Good night")
This code does not work yet. Fix it, then press Run.

Reading Any Error Message

You'll see many different errors as you learn. The good news: you can read all of them the same way.

The 3-step method

  1. Read the last line. It tells you the kind of error (like NameError) and a short message (like name 'Print' is not defined).
  2. Go to the line number. Python tells you which line it was on. Look at that line, and the one before it.
  3. Compare with code that works. Put the broken line next to a working example from a lesson. Look for the small difference: a quote, a bracket, a capital letter.

The errors from this chapter

ErrorWhat it usually means hereFix
NameError: name '…' is not definedText without quotes, or Print with a capital PAdd quotes, or write print
SyntaxError: '(' was never closedA missing )Add the )
SyntaxError: unmatched ')'An extra )Remove it
SyntaxError: unterminated string literalA quote that never ends, or quotes that don't matchUse the same quote at both ends
SyntaxError: invalid characterSmart quotes, or another odd characterRetype it on your keyboard

Errors are clues, not failures

Every programmer sees errors every day. The difference between a beginner and an expert is not fewer errors. It's knowing how to read them. Each time you fix one, you get a little faster at the next one.

On LivePython, each error also comes with a plain-English hint. Read it, follow the 3 steps, and you'll fix most errors in seconds.

Read the last line, go to the line number, and compare with code that works.

your turnPick the right idea

An error appears. What do you read first?

Think about it, then pick one.

Practice: Common errors

Solve these to lock in what you learned in this topic. Each one runs in your browser.

  1. 28.Fix the print() Errors in a Greeting ProgramEasySolve
  2. 29.Fix the print() Errors in a Receipt HeaderMediumSolve