Master Python

First steps

9 / 253
Contents

Contents

Master Python

0 of 253 complete

9 min read

The four errors you will meet first

SyntaxError, IndentationError, NameError and TypeError, each shown broken so you can fix it.

There are only a few ways to confuse a very literal helper, and after a week you will recognise them at a glance. Think of three of them in ordinary terms. The sentence is not a sentence, so it cannot be read at all. The sentence reads fine but uses a word nobody knows. Or the sentence reads fine, uses only known words, and asks for something that makes no sense: butter the water.

Python has a name for each of those situations, plus one more about the spaces at the start of a line. Every snippet in this lesson is broken on purpose. Run it, read the message, then carry out the fix in the note.

NameWhat Python is sayingUsual cause
SyntaxErrorI cannot read this at all.A missing bracket or quotation mark.
IndentationErrorThe spaces at the start of this line are wrong.A stray space in front of a line.
NameErrorYou used a name I do not know.A typo, a capital letter, or missing quotation marks.
TypeErrorThat makes no sense for this kind of thing.Mixing text and numbers.
The four you will meet in your first week.

SyntaxError

Syntax means the rules for how words and punctuation fit together. A SyntaxError says the line cannot even be read as an instruction, so Python has not attempted to carry anything out. These are the cheapest errors to have, because the program never started.

Try CodePython

Read the message, then add the missing quotation mark just before the closing bracket and run it again.

IndentationError

Python uses the space at the start of a line to show which instructions sit inside which. You have not met anything that needs that yet, so for now a space at the start of a line is always a mistake, and Python says so rather than ignoring it.

Try CodePython

Delete the two spaces at the start of the second line and run it again.

NameError

Python is case sensitive, which means capital letters count. print and Print are as different to it as print and giraffe, and it will not quietly correct one into the other.

Try CodePython

Read the last line of the message, which may even suggest the right spelling. Change the capital P to a lower case p and run it again.

TypeError

The last kind is the most interesting, because the line reads perfectly and every name in it is known. It simply asks for something that does not mean anything. You ran this one at the end of the lesson on text and numbers; this time the job is to read the message rather than to work around it.

Try CodePython

Copy out the last line of the message before you change anything. Then work out which of the two things in the sum Python is calling the wrong kind, and check your guess against the paragraph below.

The message reads TypeError: can only concatenate str (not "int") to str, which is dense, so unpack it. str is Python's abbreviation for string, int its abbreviation for integer, and to concatenate is to join end to end. The whole line therefore says: I can only join a piece of text to another piece of text, and the thing on the right was a whole number.

The error you will not get

One warning before you go on. A program that runs without complaint is not necessarily a correct program. Write print(7 * 5) when you meant 7 + 5 and Python will cheerfully tell you 35. It has no idea what you were trying to do, and no error message exists for getting the right answer to the wrong question.

This is why the working habit is: predict, run, look. Decide what you expect before you press the button, then compare it with what actually appeared. An unexpected error message is a small gift. An unexpected answer is the thing that catches real mistakes, and you can only notice it if you had an expectation in the first place.

Try CodePython

Before you run it, write down the three numbers you expect. Then run it and check. Change one operator and predict again.

Check yourself

You run pint("Hello") and Python stops. Which error is it?

All lessons in Master Python