Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
None of us are perfect which means we'll all eventually encounter bugs and errors in our code. Since it's going to happen, we should learn how to prevent and recover from them.
There are many more error types than the ones we covered in this video but just look them up as you encounter them. Trying to predict and handle every possible exception is a good way to get swallowed up by your code.
New terms
NameError
- You tried to use a name (a variable, function, or other object) that doesn't exist.
TypeError
- You tried to use a piece of data that's not the expected/right type or you tried to do something that a type of data can't do.
SyntaxError
- You tried to use some invalid Python.
-
0:00
Before we get into more serious learning,
-
0:02
there's one more thing I wanna bring up that hits a lot of beginners.
-
0:06
Who am I kidding?
-
0:07
It hits everyone.
-
0:08
That thing is how to understand error messages.
-
0:10
The more code you write, the more errors you're going to encounter.
-
0:13
It's just another part of programming.
-
0:15
We'll just use the Python shell to generate a couple of errors and
-
0:18
dissect them.
-
0:19
We'll start with a couple of the most common ones, NameError and TypeError.
-
0:24
So first thing we need to do is get into our shell again.
-
0:28
So we're gonna go ahead and type python to bring up the python shell.
-
0:32
And the first error I wanna look at is a NameError.
-
0:36
A NameeError happens whenever you try to use something that doesn't exist
-
0:40
as far as python's concerned.
-
0:42
Like right now, we haven't created any variables or functions or
-
0:46
anything like that, so we can't use any functions or
-
0:49
variables, unless Python automatically has provided them to us.
-
0:54
For instance, let's pretend we're back in algebra one,
-
0:56
and I want to try and add five to a variable named x.
-
1:02
So we'll do 5 + x.
-
1:04
And I get a NameError, and
-
1:07
the NameError that I get is that the name x.
-
1:12
So this variable here that I tried to use, is not defined.
-
1:16
That's just Python's way of saying hey,
-
1:19
you told me there was a name that's named x and
-
1:24
when I went looking for x, I couldn't find it.
-
1:30
So we haven't said what x is.
-
1:32
Sadly, programming in standard Python isn't like algebra class and
-
1:36
we can't just make up placeholder names.
-
1:39
We'd get the same error if we tried to use a function that doesn't exist.
-
1:43
Like maybe we wanna run a function to call our best friend.
-
1:46
So call_best_friend() and
-
1:49
exact same error, still a NameError.
-
1:54
But now it says that the name 'call_best_friend' is not defined.
-
1:58
And it's right, 'call_best_friend' is not defined.
-
2:02
We'll talk about how to make your own functions later on.
-
2:04
Right now, I just want you to understand what this error means in case you run into
-
2:08
it at some point 'cause chances are you will 'cause I do 'cause everyone else does.
-
2:14
Errors happen all the time to everyone, and they're fine.
-
2:18
The other really common error is TypeError.
-
2:23
Now, usually, you get this for two reasons.
-
2:25
Either you gave the wrong type of argument to a function,
-
2:28
like if a function expected a number and you gave it a string or
-
2:34
because you tried to do something that that particular type doesn't support.
-
2:39
So you can't add a number and a string together.
-
2:45
It doesn't make any sense, there's no way to add those two things together.
-
2:49
So that gives a TypeError and it tells us we have unsupported operand type,
-
2:55
and the operand is this thing.
-
2:58
Operand is a fancier, jargony way of saying you told me to do
-
3:02
an operation like plus, minus, times, divided, whatever.
-
3:07
I don't know how to do that then.
-
3:09
It doesn't know how to add an 'int' so
-
3:13
a number, and 'str' which is a string.
-
3:18
So doesn't know how to do that.
-
3:21
Now we can of course, add two numbers together, 5 + 5 is gonna give us 10.
-
3:27
And we can add two strings together.
-
3:31
'Tree' plus 'house' is gonna give us, Treehouse.
-
3:33
So the real problem isn't that neither type understands the plus sign,
-
3:37
it's not that neither type understands the operand.
-
3:40
It's that strings can only be added to strings and ints or
-
3:44
numbers of any kind can only be added to other numbers, and that makes sense.
-
3:50
How would you add the letter a to the number 5 anyway?
-
3:56
The last common error that we should talk about is the SyntaxError.
-
4:01
Now this one will probably bite you more than the other two, and
-
4:04
it's also probably the most cryptic.
-
4:07
Anytime that you see SyntaxError it just means that you wrote some code incorrectly
-
4:12
so that Python just can't understand it.
-
4:15
Now, we've already printed some information to the screen in the past
-
4:18
using the print function.
-
4:20
Print normally prints strings, so let's try that.
-
4:23
So we'll say print('Hello'), and it prints out Hello.
-
4:28
But what if I'm busy, I'm just coding along and
-
4:31
I forget my quote mark and I press Return.
-
4:36
I get a SyntaxError.
-
4:37
Oh no, SyntaxError.
-
4:39
Now, looking at the output though it's pointing with this little arrow right here
-
4:45
to the end of my word.
-
4:47
Now, did I do something wrong there?
-
4:49
Yeah, I did.
-
4:50
I forgot the quote mark.
-
4:52
I haven't use the correct syntax or way of writing the code and so
-
4:56
Python gave me an error.
-
4:59
In fact, in this case the error even says very cryptically that I left
-
5:04
off the end of the string.
-
5:06
EOL stands for end of line.
-
5:09
And this tells me that Python ran out of string unexpectedly.
-
5:14
It thought the string was still going and it never found the end of the string.
-
5:20
It kept looking for that closing quote mark and never found it.
-
5:24
So it ran out of string, and got to the end of the line and it didn't expect one.
-
5:30
Finally though, this gives us a problem that knitters and
-
5:33
programmers can come together on.
-
5:35
Running out of string.
-
5:37
Sorry for that one too.
-
5:39
We can fix those of course, by putting the closing quote mark
-
5:43
into our code, and we'd run it again just like we did the first time.
-
5:48
So awesome!
-
5:50
All better.
-
5:51
Three errors that you can now understand and survive.
-
5:55
Knowing what these errors mean and
-
5:56
how to fix them is a big part of debugging your Python code.
-
6:00
Don't let errors like this discourage you or knock you off of your game.
-
6:03
Every programmer ever has to deal with errors like this, and worse.
-
6:07
Welcome to a long line of coders shaking their fists at their screens.
-
6:12
Okay, so you've been introduced to variables and functions.
-
6:15
You've seen how to write code in a Python shell or in a script, and
-
6:18
you can decode a couple of error messages.
-
6:20
You're in great shape to actually start learning the ins and
-
6:22
outs of Python programming.
You need to sign up for Treehouse in order to download course files.
Sign up