Welcome to our Worked Exercise.
This is Dr. Charles Severance and Worked Exercises from Python for Informatics.
This material is copyright Creative Commons Attribution 3.0.
And if you need basic guidance on how to install
Python or use your text editor in general, or how to
use the command line, stuff that we'll be using in this
exercise, go back to Pythonlearn.com and work through the introductory materials.
So here's our exercise.
We're supposed to rewrite our pay program and we're going to use
try and except in this situation so that we can handle
non-numeric input gracefully. And we're supposed to print out an error
if there's any non-numeric input and otherwise print out the right thing.
So let's get started.
Let's go ahead and start TextWrangler and start Terminal program
and make it a little smaller, a little bit to the bottom.
Make this a little smaller, get rid of these.
So I'm going to start with a program that I've already got written.
And so I'll open the last one I worked with, which is
hours2, and I'm going to save this quickly as hours3.
We programmers do this all the time.
I mean, why waste time typing if I just got done typing it?
There we go. And that's why these exercises build on it, so you get
used to this notion of just kind of hacking code that you had before.
Okay, so I'll put the print of rate and hours back in.
This part from here down is pretty safe.
So I'll go into my desktop, go into Python for Informatics and say
python hours3
and I'll say, you know, 40, 10, super duper, and I say, gluh gluh, crash.
So where is it blowing up?
Well, it's blowing up on the float.
So the float is the, the dangerous thing
and this is, this float is the dangerous thing too, right?
And so, this is where it's going to blow up and now, because of this, the rest of
the program hasn't run and this is exactly the
situation where you use a try and except for.
Okay, so lets do try and we're going to just,
we're going to use a sledge hammer on this one.
We're going to put all four of these statements in the try
block, but only two of the statements are in some danger
and in a second, I'll show you what can go
wrong with this approach, but for now we're in the beginning.
e-x-c-e-p-t colon, and this is where we put
our error message, where we go print and what are we supposed to print?
A let's just kind of steal that error.
Here, please enter numeric input.
So I just cut and paste that.
So now we've taken insurance out and
if everything goes well, this except is ignored.
If anything goes wrong, and really it's going to be the
floats, one of the two floats are going to go wrong.
Away it goes.
So let's go ahead and run this.
Make this a little bigger and I will save it.
Okay.
python hours3.
Let's do it right first.
Looking good.
Now let's do it wrong.
Now I typed in a bad number and it said
error, please enter numeric input, but then it blew up.
Traceback to line 9.
This is line 9 right here.
Oops, well, that's because I didn't tell it to do, I didn't tell it to stop.
It just kept on going, right?
And so there's a couple ways we can solve this.
We could solve it cleanly but I'm just going to be crude at this point.
Now I'll put all this stuff in the except
because that way if it dies,
it's going to run this
and then the program's going to be done.
Okay? And if it succeeds, it's going to fall through and
it's only going to do this code if that other code worked.
And I will show you in a moment the fallacy and
why this isn't the best way, but this is proven effective, right?
We're early on, so all we have to do is find a solution.
In a bit, we'll talk about what the better solution is.
So here we go, python hours3.
So we type fred.
Kabloom, it dies.
So it died on this line right here and then it
transferred to the except and ran this and the program was over.
And if we give a good one and then something
bad, it, it, this line dies, and then it continues on.
So let me show you a couple of fallacies or some mistakes in this.
So watch this.
I'm going to make a mistake here.
Print
I'll say print r comma h.
So I'll print r comma h.
So I'll type python hours: 40.
10.
Error, please enter numeric input.
So what in the heck was happening?
So like, it is numeric input.
Where did it die?
So what, I'll tell you what, I'll just show you
what happened here and we won't worry too much about this.
It came into the try.
It did this.
The 40 is good.
So that worked.
The rate is good.
So that worked.
What worked was r is not defined. But it turns out that this is another error.
It's just a different error.
It's not the same as this one and we just
didn't type it right and so it jumps to the except.
Doesn't do any of this code but it jumps to the except.
So that's why it was numeric input but away we go, okay?
So, that's not so good.
You don't really want to put too much code inside your try and except.
So let's take a look and do this one a little better.
I'm going to cut this back out, I'll move it down to the bottom.
Now I gotta de-indent it.
I'll fix this rate in hours.
Indent.
I'm trying
to get my indents right.
Okay.
So now my indents are right.
So let me go ahead and run the program just to make sure I'm not crazy.
40, 10, that's good.
50, 10, that's good.
So I'm good.
Okay, the problem now is I've reintroduced my error
that if I give it a bad thing, okay?
So let's take a look at the kind of things
that we can do to make this a little more elegant.
So so we're right here.
This is the problem.
We, we need to stop.
We don't want to continue at this point.
If there was a thing that we could say just, just stop here.
If there was such a thing in Python, we'd
be fine but that would be bad Python syntax.
But there is such a thing in Python, if
you want to quit your program, you type quit.
It's a function.
We haven't covered functions yet.
We will soon.
It's a function that never comes back.
Most functions come back, but the quit function does not come back.
So that when it comes down here and it
prints this, the next thing and then it doesn't continue.
So this is a way of saying we are done.
So now let's run this and put in some bad information
and it indeed quits and so it doesn't continue down here.
Let's make sure it works with good data.
Put 4 hours well, okay, I guess that's right.
So let me comment out my debug print statement here and finish it up.
And run it one last time.
Error, please numeric input.
Good hours, bad rate.
That's good. And good hours, good rate, and that's good.
So we've accomplished this one.