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
You aren't perfect. Nobody is. Let's take a look at how to deal with errors when they happen...and they will!
-
0:00
I've got some bad news for you.
-
0:02
You aren't perfect.
-
0:04
Nobody is.
-
0:05
You're bound to make a mistake when you're coding.
-
0:07
And even though we just agreed that making errors is a good thing.
-
0:10
It can still feel a little overwhelming.
-
0:12
Part of the reason for this is that you don't yet know the rules of the language.
-
0:16
These rules are called syntax and
-
0:19
I can pretty much guarantee you're going to make a syntax error.
-
0:23
In fact it'd be super weird if you didn't.
-
0:25
So let's do this.
-
0:26
Let's force some errors and then walk through the fixes together.
-
0:29
This error fixing skill will help assist you as you pick up the syntax of
-
0:33
the language.
-
0:35
Okay, so let's launch your workspace.
-
0:39
So, remember when I said that case matters?
-
0:42
So this function name here, print, let's capitalize that p.
-
0:50
Now one nice thing that just happened, did you notice how the color changed?
-
0:53
So see there's regular p, it's red.
-
0:55
Here's capital P, blue.
-
0:58
Our editor knows about the print function.
-
1:00
Note, when I change the file there is a little orange dot up here.
-
1:05
Do you see that?
-
1:06
That's letting me know that the file has been edited, but not yet saved.
-
1:11
So, what I'm gonna do, is I'm going to save this file.
-
1:14
You come in here and you choose Save and you can see that I'm on a Mac so
-
1:17
this is the sign for Cmd+S.
-
1:20
So I believe on a Windows it's Ctrl+S.
-
1:22
So you can go ahead and click Save and now you see the dot is gone.
-
1:26
I wanna make you aware of this totally common problem.
-
1:30
So you write amazing code and you forget to save it and
-
1:33
then by pure instinct you just smack your face on the keyboard trying to figure out
-
1:37
why your script isn't working the way that it says it's supposed to be working.
-
1:40
And it's all because you forgot to save.
-
1:42
And sometimes you even luck out and your nose hits the command key and
-
1:45
your brow hits the S and you fix your problem, but that's rare.
-
1:48
So mind the dot.
-
1:50
So since case matters we know that lowercase print is different than this
-
1:54
capital P print.
-
1:55
But let's keep this here.
-
1:57
Let's see what happens when we run it.
-
1:59
So again, we're gonna come down here and we're gonna say Python.
-
2:02
We're gonna call hello.py.
-
2:06
Yikes, what's that?
-
2:08
That's right.
-
2:08
We've made this happen on purpose.
-
2:10
So, this is what is known as a trace back and
-
2:13
it helps you follow or trace the path in the code that caused the problem.
-
2:18
Kinda makes you feel like a detective, right?
-
2:20
Trace down that perpetrator.
-
2:23
Now we only have a one line script, so it's pretty clear where the area is.
-
2:28
But this can get pretty big pretty quick.
-
2:30
So what this is saying is that the file hello.py on line one and
-
2:35
if you look up here at this line numbers in the gutter.
-
2:39
So if you come here, you can see that I can add new line numbers there and
-
2:43
they go up, so that will help you find those.
-
2:45
I'm gonna get rid of those.
-
2:46
So, we've got an error in line one, and
-
2:49
if you come down to the very bottom you'll see the error.
-
2:52
And we have a name error.
-
2:54
And here it says the name Print with the capital P you notice, is not defined.
-
3:01
So, name here is the name of the function print.
-
3:06
We'll learn here in a bit how to define our own names.
-
3:10
Basically the interpreter here is saying, what you talking about?
-
3:13
There isn't anything defined as a capital P print.
-
3:17
Remember, case matters.
-
3:18
Capital P print and lower case print are different.
-
3:21
Different key strokes for different folks.
-
3:24
So let's fix this, let's go ahead, come up here, change this back to lower case p.
-
3:29
There we go, back to red.
-
3:31
All right, so this print function here is called, I briefly explained earlier that
-
3:38
you call a function by providing an open parentheses and a closing parentheses.
-
3:44
Now note here, if I put my cursor at the opening parentheses or
-
3:48
the closing parentheses, it will highlight.
-
3:50
It will show me how they're balanced.
-
3:52
And it is important that they're balanced, it's part of the syntax of the language.
-
3:56
The syntax states that for a function that an opening parentheses and
-
4:01
everything in between it and the closing parenthesis are arguments.
-
4:06
So, what happens if we forget the trailing parenthesis?
-
4:11
Let's go ahead and get rid of that.
-
4:14
This is invalid syntax, right?
-
4:16
Where would the function call actually end now?
-
4:18
It doesn't, right?
-
4:20
So I'm gonna go ahead, I'm gonna save this.
-
4:22
See the red dot, now the red dot's gone.
-
4:25
So I'm gonna come down back to my terminal.
-
4:28
And the terminal actually has history as well and I can use the up arrow,
-
4:31
and we'll run Python hello.py.
-
4:33
File hello.py line 2, what, there's only one line.
-
4:39
So we have here a syntax error,
-
4:42
unexpected EOF while parsing.
-
4:47
Now parsing is the way that the interpreter breaks down what you wrote
-
4:49
into something that it understands.
-
4:51
It had a problem understanding us because we didn't follow the rules of
-
4:56
the language.
-
4:57
Now, if you don't know what EOF means and you probably don't, what would you do?
-
5:03
Well, what do you do these days when you don't know what a term means.
-
5:08
What do you do?
-
5:09
Yeah, that's right.
-
5:10
You just ask Google or you ask Alexa, or whoever your preferred oracle is.
-
5:15
Let's go over there, I'm gonna use Google for right now.
-
5:17
Hi Maya.
-
5:18
So, I'll say what does EOF stand for?
-
5:24
In computing, end-of-file, commonly abbreviated EOF, is a condition and
-
5:27
a computer operating system where no more data can be read from the data source,
-
5:31
end-of-file.
-
5:32
Well, that makes sense,
-
5:33
it reached the end of the file without finding the closing paren.
-
5:38
I want to reiterate here that you are never alone in your errors.
-
5:41
Millions of people are coding and making mistakes.
-
5:44
You'll find answers to their questions if you just search for them.
-
5:47
The internet is pretty amazing, isn't it?
-
5:49
Now, for course specific things,
-
5:51
remember that there's a wonderful community here to ask.
-
5:54
And you can also search the archives on questions and answers.
-
5:57
So let me point out one more common gotcha before we get cooking.
-
6:02
This is one that happens all the time.
-
6:05
So, in Python you can make a string just like we did here with double quotes,
-
6:11
or you could also use, I'm gonna put this paren back,
-
6:15
you could also use single quotes.
-
6:18
So this is also valid, single quotes.
-
6:22
It really is just a question of style, often though,
-
6:26
the error that happens is that there is a mismatch.
-
6:29
One single quote and one double quote.
-
6:32
Now let's do that, one single quote, and we'll trail it with a double quote.
-
6:36
Now a careful look at that closing paren will show you the problem.
-
6:39
Notice how when it's correct it's black, and
-
6:43
when it's not it's that teal color, the same color as our string.
-
6:48
Now, that's because the string hasn't been ended yet.
-
6:51
It's waiting for us to finish with a single quote, and
-
6:55
it's totally fine to have a quote and a paren in a string.
-
6:59
So there if I put that in, that's valid right?
-
7:03
But since there is no closing single quote,
-
7:05
they're part of the unclosed string.
-
7:08
So let's go ahead, let's make our error happen.
-
7:10
I'm gonna save this.
-
7:13
And I am gonna type clear down here because that will clear the console stuff
-
7:18
that's there and I will press up and get python hello.py.
-
7:24
Here we go, we got a syntax error and we have EOL which is end of line,
-
7:29
while scanning the screen literal.
-
7:33
And you can see that there is an arrow here actually pointing
-
7:37
to where it expected the ending of the string.
-
7:39
Now, this is where you want to really inspect closely,
-
7:43
almost character by character.
-
7:45
I want you to hone your inner detective, aha,
-
7:49
elementary dear Watson, mismatched quotes.
-
7:54
I hope you now feel a little more prepared for
-
7:55
when you encounter those errors and you will.
-
7:59
Don't let them get you down.
-
8:00
When you see those tracebacks, try to remind yourself hey, look at me,
-
8:04
I'm learning.
-
8:05
And with that confidence, let's get to writing some code.
-
8:09
That is right after you take a quick break.
-
8:11
Taking breaks is super important for your learning.
-
8:13
You'll thank me later.
-
8:14
A lot of information just went into your brain.
-
8:17
Let it sink in.
-
8:18
Give a nice stretch, grab a snack, pet your cat, water your plants,
-
8:21
refill your coffee or whatever it is that you like to do when you're taking a break.
-
8:25
It doesn't have to be long.
-
8:27
Come back refreshed and
-
8:28
ready to name some things cuz that's whats up next, creating variables.
You need to sign up for Treehouse in order to download course files.
Sign up