0:28
So to kind of get things started,
I want to kind of recall a quote from the 1700s.
To err is human, to forgive is divine.
Now, as you progress with this class,
you'll see this quote is actually true in two different ways.
First, you'll be reminded you're human.
As you make errors,
Python's going to type them out in bold red letters you've messed up.
But there's a second kind of take away from this quote.
If you watch kind of popular movies, there's always a worry that computers
going to take over and Skynet is going to become self-aware.
What you're going to see is that computers are not even close to being God-like.
In fact, I think more of them as a small child that will throw a tantrum
whenever you make the simplest of errors.
1:18
So what we have here loaded is actually
the program that was linked in the previous pause.
You click on that link,
it will open up CodeSkulptor 3 with this program loaded on it.
So if you didn't do that, stop, go back, reload the program.
This program has a sequence of
errors that we're going to work through during the rest of the lecture.
So let's just look at the first error here.
So I'm just going to run my code.
And I run it and, of course, there's an error.
It's highlighted.
Line 16, it says there's something wrong with line 16.
And so the first thing you should do is don't panic, okay?
Read the error message.
The error message may give you hints as to what's gone wrong.
Here it says line 16 syntax error.
A syntax error an error where you failed to write a valid Python program.
Basically, CodeSkulptor is trying to go through and
kind of determine, based on what you typed in, what you intended to do in Python.
And it just doesn't understand what you're doing.
2:16
In line 16 we've made kind of the most common syntax error you're
going to see me make at least.
I've written a lot of Python 2 code.
This is the syntax for print in Python 2.
Particular, it's print and then the string you want to print.
In Python 3, print is a function, so
you have to enclose the string you want to print in parenthesis.
So I'm going to do that, and put parenthesis around it.
I'm going to hit Run, and there you go.
To err is human, to forgive is divine, excellent.
2:49
Okay, let's move on to our next example.
So, what I'm going to do, is I'm going to comment out the first example.
And I'm going to uncomment the second example.
It's this print statement here.
And I'm going to hit the Run button.
And sure enough we get another error.
And again, it's a syntax error, except now it says bad token.
And inside the single quotes here is a double quote.
So it's complaining that there's something wrong with this double quote.
So let's go back and take a close look at line 24.
I say print, and I use parenthesis like I need to for
the print statement in Python 3.
And I want to print out this string.
And the string is quote to err is human, to forgive is, uh-oh.
I didn't put a closing double quote there.
That's the problem.
In fact, what I'm going to do is I'm going to correct this.
And as you watch me correct it,
I want you to pay close attention to what happens to the cursor.
So I deleted that closing parenthesis for a reason,
because I'm going to put in the second double quote.
But now, watch what happens.
What you can see here is that this parenthesis over here turned green,
and this turned green.
And that's CodeSkulptor telling you that your parenthesis are actually
correctly matched.
If I go back and I re-enter the incorrect program,
what you're going to see here is this parenthesis is red.
And that tells you that it already detects that there's something wrong with
your program.
So when you see a red parenthesis you should immediately go look and
say uh-oh, I think there is something wrong with my program.
And check the syntax and make sure you've entered things correctly.
All right, so let's put in the double quote, put in the closing parenthesis, and
hit Run, and there it is.
To err is human, to forgive is divine.
4:24
Okay, let's go in and look at another error.
So first thing I'll do is let's comment out the code that had our second error.
And I'm going to go through and uncomment the code that has our third error.
Instead of going line by line and
removing the hash marks at the beginning of the line,
what I'm going to do is I'm going to actually use a command in CodeSkulptor
that block uncomments a collection of lines.
In Codeskulptor I do the Ctrl+Shirt+K.
And notice it uncommented all the lines simultaneously.
If I want to put those comments back, I can say Ctrl+K.
Ctrl+Shift+K.
And now, let's see what happens when I run it.
So I hit Run, and it says syntax error line 40, bad input print_quote.
So let's look at line 40.
Now, we haven't done assignment statements yet.
We haven't done if statements yet.
So this is going to be something you're going to cover in the next
couple of weeks.
But this is going to illustrate an error that you'd look for
whenever you don't see an obvious syntax error.
Line 40 looks fine to me.
5:25
So remember, what causes syntax error?
Python is trying to figure out what you intended when you wrote your code.
And one of the rules for an if statement is, that both the if clause and
the else clause have to have a statement in the body.
In other words, you can't leave it blank.
And up here, my else clause, there's nothing else in here.
I didn't put anything in here to do whenever
basically the else clause is executed.
So to do that, what I can do is I can just put in pass.
Pass is a statement which tells Python do nothing.
6:26
In the fourth error,
this error's going to happen when Python tries to actually execute your program.
The first three errors it didn't even know enough to figure out what to do
with your code.
Now, Python's tried to make some sense of your code, and now it's going to try to go
through and actually execute your code and do something.
So this error is going to correspond to Python saying I'm running your code, but
I don't know what's going on.
So let's uncomment the two lines for the fourth error, and let's hit Run.
And it comes back and it says line 58, a name error.
It says popequote is not defined.
7:10
Name errors mean you probably misspelled something.
That's the most common cause of a name error.
Here I had an underscore in popequote.
It's Python standard kind of convention
to put underscores between kind of several words that go in a single variable name.
So here I was saying pope underscore quote, and
I just forgot to put that underscore in.
So I'm going to do that.
And I'm going to run it.
And to err is human, to forgive is divine Okay,
let's look at our final example of a common error in Python.
7:45
So the previous error happened when Python was executing your program and
didn't know how to proceed.
This is going to be the same kind of error.
So the error is going to be in lines 67 and 68.
Let's uncomment them.
And before I run it, I want to kind of explain what I'm trying to do here.
I'm trying to print out Joe is number one coder.
Clearly.
So I have Joe ranking as one, and then I go through and
say Joe is number one coder.
So I'm trying to kind of add all these together.
8:17
So let's run it, and then I'll explain what the heck's going on.
So I run it, and the error message says TypeError,
says cannot concatenate 'str' and 'int'.
So what's going on here is I'm trying to take advantage
of a feature in Python that lets me take the addition operator +, and
apply it not just to integers, like 1+1 is 2, but apply it to strings,
and take strings and kind of smash them together like Joe and coder.
And I concatenate them.
So in Python, Python likes to take operators on basically one type and
kind of interpret it to also do something interesting on another type.
So, Plus on strings takes them and concatenates them, pushes them together.
So what I want to do is I want to say Joe is number, and then take this one here and
kind of smoosh it together with it.
Python says you can't take a string and an integer and add them.
9:27
Okay, that's enough examples of errors.
As you write more code in this class, you'll come on other errors.
And so what we've done to kind of help you to work through this process,
we created a class reading that follows this video which has many more examples
of common errors in Python, and then shows you how to fix those errors.
So if you get stuck on a error as you're coding, take a look at that reading.
I think you'll find it to be very helpful.