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