For more information on concepts covered in this lesson, you can check out:
Make Some Mistakes
00:04 It’s really difficult to not make any mistakes while programming. And in case you haven’t made any so far, I’m going to go ahead and have you practice making a few of them just to get an idea of what’s happening.
So it starts with a quotation mark and then
Hello, World, but there isn’t one that closes the quotation there. If you save that and then run—again, you can press F5—you get something new here where it says
unterminated string literal
(detected at line 1). Depending on the version of Python you have, this message may change. Python 3.10 has added much more explicit explanations of errors, which is really kind of nice.
01:32 In this case, it’s been reformatted and says something a little nicer. So what is a string literal? Well, a string literal is what you’ve created here, but in this case you haven’t closed it or terminated it with the additional quotation mark. To create a string literal, you have to enclose the text that you want in both of the quotation marks.
It can be either the double quotation (
") or a single quotation (
'), and you’ll spend a lot more time on this in an upcoming course all about strings. To fix this
SyntaxError, you would need to add that other piece and save.
right where I clicked on
string literal here, it actually highlighted where it began, which is kind of an interesting feature too. So it’s kind of letting you know that, “Hey, this is where you started this thing, but you didn’t terminate it, didn’t complete it.” Okay, now there’s a different type of error.
03:08 When an error occurs, Python stops executing the program, and then it’s going to display several lines of text that is known as a traceback. And it’s going to show a bunch of useful information about the error.
What’s very interesting about it: it’s best to read them from the bottom up. I’m going to have you go back into your file, and this time, you’re going to create a
RuntimeError, and you can see the difference between the two. To create a
RuntimeError, try this instead: remove both quotation marks.
you’ll see this happen. So this is called a
RuntimeError. And again, to read the traceback—this thing happening here—it’s best to read it from the bottom up. So it says right away, this is a
It’s valid as far as the syntax of it, the way it’s written out, but
Hello doesn’t exist, so it’s not been defined yet. And that’s something you’re going to do in the next lesson is learn how to define variables. And if it were to continue, it actually would have the same problem with
World. Again, you can read from the bottom up. So it’s a
Hello is not defined. And then it shows you that statement where that happens.
04:47 And then it actually kind of goes a little further up here. This is the file that it’s reading from. Sometimes Python projects have multiple files, and the traceback might lead into another file. So in this case, it says, “Okay, where you have this saved in the name of the file.” And that actually tells you which line it is. Again, you’re not going to have such simple programs coming up.
In the case of a
SyntaxError, it won’t let you even run it because it doesn’t look like normal Python code. It is not following the correct syntax. In this case, this is a completely different error, where it read through everything, but when it went to run, it, it couldn’t find
Hello being defined anywhere.
Become a Member to join the conversation.