[MUSIC]
Hi, I'm John. Welcome to my first video for this course.
So, we're going to be looking at a bunch of examples.
First on errors.
When you get an error message, how do
you understand what it means and fix the problem.
Second we're going to look at a few probing tips
that should help you when you're writing your own code.
So here's our first example. Let's run it and see what happens.
It starts to print out this message about the volume of a cube,
but then has a name error saying that volume is not defined, kay.
It highlights the problem line here, where in fact we
are using the function named volume to do our computation.
But where did I define volume?
Well, I didn't. I defined volume cube, kay.
here I should be calling volume cube, so let's fix that, kay.
So, now let's run it again and make sure. Oh, whoops!
We have another name error.
This time, I used the name Sidde here in
this line instead of the variable named side, side.
Kay, so a typo here.
Kay, so running it finally it works.
So, notice that we get a name error when we misspell either a
function name or a variable name here and don't have a definition of that name.
Alright, let's see what happens with this next example.
We'll run it and well, once again we get
a name error here, name random is not defined.
So well, we're trying to use random.randrange to roll a die.
Hmm, what's the problem here?
well, here let's go to the documentation, kay.
And we can scroll down here to the random
module, and it tells us well, yes random.randrange is right.
But whenever we need to use it well, yeah, import
random, we forgot that and it tells us up here.
You know, whenever we're using anything from this module, import random.
We forgot to do that in our code here so let's add that.
Yeah, we didn't provide that
definition of random that it's a module here, so
let's try this again, running it and it works fine.
It, it, rolls our dice and each time we get a different value for our pair of die.
In this example we want to compute the volume of the sphere.
Let's see what happens when we run it.
We get a attribute error. Module object has no attribute pi.
Hmm, well we're using math.pi and I'm importing math correctly.
So what's the problem?
I thought pi was defined.
Let's re-, let's go to our docs, and we've got a little delay on load right now.
let's search for pi and there we go.
There's math.pi, but it's lower case, okay.
All of our names are case-sensitive, so I misspelled this, and any time
you misspell the part after the period,
that's going to give you an attribute error.
So changing that and rerunning, it now works.
In this example we want to calculate the area of a triangle so, well.
We get a type error, area triangles takes exactly two arguments but one was given.
So let's look at our function call which is here, area triangle b.
Well, in fact, I did give it one argument.
instead of the two that it's expecting I forgot
to pass an h, so let's add that. Okay, and re-run.
[SOUND].
I get another type error. Oh, hmm?
But it, at least it's different. we fixed that one problem.
Type error can't multiply sequence by non ends of type number, huh?
Alright, let's see what it's complaining about.
Okay, so I am doing a multiplication like it's talked about.
As we need to in this formula.
But, what's going on?
So, let's look up at, at what's actually being printed out, and it says,
the area of the triangle with base five and height two plus two is.
Height should be four.
Why isn't it adding two plus two there to get four?
Well, the problem is we have strings here not numeric expressions.
This is
simply the string two space plu- two space plus space two.
It's not a mathematical expression resulting in four.
So that's why this is resulting in multiplying a
sequence because, well, it's trying to multiply a sequence base,
which is a string and a string is a
kind of a sequence by something that's not an integer.
Well, height here is also not an integer.
Turns out you can actually multiply strings from an
integer, but that's a whole separate thing, separate operation.
Kay, so let's just get rid of the double quotes here.
Turn these into mathematical expressions and rerun and we get what we want.
So, next here we have a little function
that's supposed to simply check whether we're giving
it Mary, and if so it gives us a message, that yes it has Mary, otherwise no.
Okay, so let's try running this, 'kay.
Syntax error, bad input on line one.
[SOUND].
Quote, you know, and then quote down here on the next line.
Kay, so it tells us there's a syntax error where it is roughly, on what line.
Okay, and the fact that, well, it's trying to tell us here that it's
it's at the end of the line, where, where it's encountering a new line.
Kay, and so here it's just kind of giving us a new line there.
Kay, so that is hinting that
it must be at the end of the line here.
And in fact we forgot to put the colon on here.
Remember, every function header begins or, ends with a colon.
Just like a every if here ends with a colon, else ends with a
colon, anytime that you're going to start a block of code, kay, so, fix that.
Let's see it run now.
Oh, another syntax error.
Bad input on line two now. Okay, we have if x is equal to Mary
there's something wrong with the equals. What's wrong with the equals, hmm?
Let's go back to the documentation here. And we could, let's try comparing things.
Let's see, where would I find something about comparing?
So, compare, oh, comparison operators there.
let's see.
Oh, the comparison here is double equals not single
equals kay, very common mistake. Single equals is assigning a variable,
double equals is comparing two things. Okay, so
let's try this again here. Oh, name error, name Mary is not defined.
Oh, kay, yeah, down here I gave it.
Well, Mary is not defined, I, I meant to pass in just a string.
Okay?
Here, note that note the color, okay? Well this is kind of useful here.
So, in this code here where we have
some strings, note the strings are color-coded red.
And this is not color-coded red.
It's just black like any other name.
And well, notice over here we actually have purple stuff too, for keywords.
Kay. Notice when you're typing,
whether things are turning up the color that you wanted.
Kay, and so here, basically just another kind of syntax error.
I meant to give a, meant to give it a, a string instead of a variable name.
So let's see if I'm good now.
Oh, good.
You know, it found Mary and then Fred, well, is clearly not Mary.
So, fixed it.
So let's turn to the issues of code style and readability now.
So, just trust me and that this code actually works.
So, we have some code, what does it do?
Def area a, b, c and some math here.
Not really clear, you might not know this formula.
You know, if somebody else is looking at this
code after you've written it, they don't know what you meant probably.
In fact if you're looking at this a month
after you wrote it, you might not remember either.
So, what should we have done?
We need to provide more information to actually explain what's going on here.
The first thing is what is this supposed to be doing?
Kay, let's make that clear.
So, this is actually supposed to calculate the area of a triangle.
Kay, so let's give it a more useful name. Area of a triangle and I'm going to
call it area triangle_sss to be sure that it's taking three sides, okay?
And so well, no matter how long a name should, can be you don't want to make it
too long, it gets just kind of unwieldy. You can provide
more information in what Python calls a documentation string, or a doc string.
And in Python, that traditionally goes right here, right
after the function header, and it's put in a string.
Kay?
Even though this is just a string, we
traditionally put it in three quotes, three double quotes.
what the three double quotes allows is that, it allows a string
to go over multiple lines. Kay?
Now, once you go into a document string, you should talk
about what are the inputs what's the output and how those relate.
So, here we could say for example,
that this returns the area of a triangle
given the lengths of its three sides. Kay?
Now, the documentation string tells you what it does, it
does not tell you how it actually does that, kay?
So we've got some weird math here.
You might recognize it, you might not, kay?
It's actually a standard formula known as Heron's formula.
Kay. So, I'm going to write that down.
You know, somebody can look up Heron's formula and
say aha, you actually typed in Heron's formula correctly.
Kay? Well, I'm going to put this in a comment.
A comment starts with one of those hash lines or pound signs.
Kay.
And note that I'm not putting this
in the documentation string, kay, this is different.
This is telling somebody, how I'm calculating
this, as opposed to what I'm actually calculating.
Kay, so that's already better. Somebody could understand,
given the English and the better name, what's going on.
But still let's, let's improve this here.
A, b, c, you know, those are kind of poor names.
What are those supposed to be?
They're supposed to be sides, but let's make this clear.
Let's rename them side one.
And I'm going to add in some space that is for readability, too.
Side two and side three and be
sure to do that consistently.
[SOUND].
Alright, and similarly S, well that,
that's kind of a weird name. What is that?
Well if you add up the three sides of
a triangle and divide it by two, what is that?
Kay, adding up the three sides, that's the perimeter.
Divide by two, that's half of a perimeter.
In other words
it's the semi-perimeter.
Kay, so that's why it's traditionally called s.
Let's actually give it a longer name called semi_perin.
Let, you know, more descriptive. Add a couple more spaces
in here for readability and rename consistently here, kay.
Be sure of the consistency.
[SOUND].
Now one thing you can see that's not
very readable is now I have a really
long piece of code, a really long
line. Well you can break that up,
kay. Notice the indentation.
So, I have the one formula within the square
root that's broken up on four lines so it's
more readable and, and in fact they stack on
top of each other, four things that I'm multiplying.
So, this is definitely a much more readable version
and somebody could read this and understand what's going on.
So, this little program here is supposed to tell us,
you know, what is the favorite thing of our various instructors.
So, Joe loves games, that's this course.
Scott loves ties, as you should be seeing from his videos.
And I love the outdoors but, well, I'm here in front of a computer all the time.
So, well, let's run it.
And, well, it reports that I love the outdoors.
But, hm, Jeannie, my wife,
who's not an instructor and none. Where's that none coming from?
Well, first of all you know, note that because Jeannie is not
one of these options, it doesn't meet one of these branches here.
It just falls through and at the end of the function there's nothing left to do,
so it just returns a special value called none which signifies that there's
no value there. Kay?
Now that seems kind of strange, and in
particular in a larger program, if this was just
a piece of some larger thing you'd probably have
a hard time tracking down where the problem was.
Kay, so we'd like to have some way of saying, hey something's wrong
here, you gave me a valid, an invalid input that I wasn't expecting.
and so here is one really simple
way of doing it. Just make sure that each of your ifs has
an else case, kay? Else, kay, some other inner
value of instructors here let's
print out a message you know,
favorites, saw invalid instructor.
and here's our value Jeannie. Whoops, instructor.
And so we can run that, and it gives us this message saying, hey, this function
saw an invalid input, you know, now I know where to look, and where the problem was.