For more information on concepts covered in this lesson, you can check out:
Make Some Mistakes
00:00 Okay. It’s time to 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.
The first type that you’ll experience is called a
SyntaxError and the
SyntaxError occurs when you write code that isn’t allowed in the Python language.
The language doesn’t understand what you’re trying to do. I’m going to have you go ahead and create an error in the code that you’ve already written. It’s practice creating that
I want you to reopen your
hello_world.py file. And at the end of it, go ahead and remove the second quotation mark.
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.
And you’ll see more of them throughout the course as we continue. But what you might see in an earlier version of this is a statement that says
EOL stands for end of line.
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.
02:14 And then it can run and print out.
So that’s considered a
SyntaxError. And you might’ve noticed that when I did that and saved and then ran it again, that if I click on this,
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.
The other type of error that you’ll experience in programming is called a
RuntimeError. IDLE catches syntax errors before the program even starts running.
03:02 But in contrast, runtime errors only occur while a program is running.
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.
03:40 So what’s going to happen here is this is valid code. It understands its syntax, but something different is going to happen. So go ahead and save that. And when you run it,
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
There is a name for something you’re calling for to be printed. It’s called
Hello, and it’s not defined. It doesn’t know, you know, what
Hello is or what it’s referencing.
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.
05:08 This one is just a single-line one, so it’s pretty straightforward where the issue is.
But that’s how traceback works. And you get to practice them much more as you continue along. So do you get the difference between a
RuntimeError and a
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.
Okay, it’s time to do a little practice on your own. As a bit of review, try writing a program that IDLE won’t run because it has a
Then write a different program that crashes only when it’s running, because it has a
RuntimeError. Okay, next up, you’re going to learn all about creating variables.
KHiroSanchez As far as I know, IDLE will show this popup when you try running a Python module from a separate window as opposed to running code interactively in the REPL. Anyway, IDLE should highlight a problematic line to draw your attention to it after you rerun the file or check the syntax by choosing Run | Check Module from the menu.
Become a Member to join the conversation.
KHiroSanchez on March 7, 2023
Why can’t I get my syntax error to show on my IDLE Shell? Why am I getting pop-ups?