I want to show you one more kind of conditional.
It's a little bit different.
It's not a bit of code structure that you make, it is,
it is dealing with the fact that some things may blow up.
Like, if you read a number from a user and you try to convert it
to a floating point number, as you may
have already done in some of your homework,
it can blow up.
You know it's going to blow up, but you don't exactly want it to kill your program.
So, the concept of try and except are, hey, this is a dangerous thing.
I know it might blow up.
I know exactly what it might blow up, but I don't want to die
I don't want to stop my program when it blows up.
I want to continue.
And that's the purpose of the except block.
So, here's a little bit of code.
And, you know, it's, we've done this code before.
This is code that's kind of similar to, like
your rate and pay homework, where you read a string
using raw input, you converted it using float, but
then if you typed in Fred, the thing blows up.
So we're kind of simulating that right here.
So here we have a variable astr called Hello Bob,
and then we try to turn it into an integer.
And then we're going to print that out, and then we have
another string called one, and that has the letters 1, 2, 3.
We convert that to an integer, and then we print that one out.
The problem is, is that when this runs,
this is going to fail.
It's going to fail with this traceback, okay?
And the problem is, is when the traceback happens, the program stops executing.
The traceback is Python's way of asking you, hey, this
would be bad, I don't know what to do, I'm stopping.
So that means that the rest of your program is gone, right?
It,
The fact that we had stuff down here doesn't matter.
This line died with the traceback.
It stopped.
It doesn't, like, give you a traceback and then keep going.
It gives you a traceback, and that's the end.
Now this might be something, instead of
just the string, Hello Bob, which is insane.
Data might have come from a raw input, where the user was typing, and you say,
give me a number, and they type something
that's not a number, and this would blow up.
It's like, hey, I know it's going to blow up.
The problem with this is that you don't, oops, erp, clear the thing.
Oh and now we have to start it on fire again.
Okay, it's on fire.
The problem is, is that in a sense, this program is you.
If you recall, we have you as the, typing these commands
into these scripts, feeding the central processing unit, answering the question
what next?
So you should take it a little personally when your program gets a traceback.
because that means you, in the form of your program, have been vaporized.
And you're not present to give any more instructions.
It stops.
It stops dead in its tracks. You are gone.
So, we want to make sure we control this behavior.
We know it might blow up,
and we want to capture the situation where it does, and execute alternate code.
Okay.
So here it goes.
It's a bit of syntax.
I mentioned that it uses the try and except keywords.
These are reserved words in Python. And then it's a little indented block.
So, astr equals Hello Bob, great.
Try means, we're about to do something dangerous, let's take out some
insurance policy on it.
And that is, we're going to convert this to an integer.
Take astr, convert it to an integer, put it in istr.
If that works, great, we'll just continue on, and ignore this except.
If it blows up, we're going to jump into the
except block, and then we'll have alternate substitute code.
In this case, I'm going to set the variable to negative 1 as an indicator.
Then I'll print it out, and I'll do it again.
Try this code, and away we go.
So, when this runs, we know exactly how it's going to run.
It, whoa, woop, come back.
We'll set this string, the try takes out the insurance,
this blows up, so it runs down to here and runs this part, and then it'll
print First minus 1. And it sets the string to 1, 2, 3, not
123, but 1, 2, 3 as a string. It takes out an insurance policy.
This time it works, and that puts istr is going to be 123,
so we don't run the accept code, and so out comes the
second 1, 2, 3, okay? So the try is, take out insurance on this
little bit of code, and if it fails,
run this alternate code. If not, skip the alternate code.
So it's kind of conditional.
If you put multiple lines in the block between
the try and the except, it runs until one dies.
So it doesn't come back, okay?
It's not taking insurance out on, separately, on all three statements.
It's like, here's a block of stuff, and if anything blows up,
stop. And the things that run do run.
So if, this is really kind of bad code,
because you really don't want the print in here.
It's actually a good idea on the try except to have as little in the
try block as you possibly can, so you're real clear on what's going to fail.
but, so here we come in, shh, it's Bob, so it's going to fail.
We run this.
That runs successfully.
This blows up, so it quits and jumps into the except blocks and continues.
The point is, is that this code never executes, never executes.
The other point is, this code does execute.
Just because this blew up, this is already executed,
it might have done something other, more complex than
print Hello, okay? So, so there you go.
So, if we look at this kind of in a picture,
we, we set this through the try block, it runs, it runs.
And the, the try except kind of has this escape hatch that says, if there is
a [SOUND] explosion somehow, then it runs this
alternate code and then it comes out and finishes, okay?
And, again, this, it doesn't go
back and finish the block, and it doesn't undo the work that is done by that.
So it doesn't un-execute it.
If it executes and works, it just keeps on going, then
it blows up, and then sort of flushes its way out, okay?
So here's an example of how you might do this in a running program, like the
programs that you're about to be assigned, where
you're supposed to check for user input having errors.
So, here is a little conversion of a number, and
and so we're saying, you know, enter a number, and we're putting a string into rawstr.
It's a string, and and so, we don't know.
And here's where we're going to convert it into an integer,
and we're just not sure if it's going to work or not.
So, we know how int works. It either converts it or it blows up.
So we know
it's going to do that, we just don't
know what the user's going to type, we don't know.
So we have to take out insurance on it. So this runs,
and then we do a try, and then we try to convert it,
and if it works, it's great, and if
it fails, it runs this and sets it to negative 1.
And afterwards, we either have the number or negative 1.
And so, if the person enters 42, it says Nice work.
Well, let's show you where it runs.
If the person says 42, it runs through here, gets the string 42, converts that
to an integer, skips here, and then says, Nice work, and that's how it runs.
If, on the other hand, they type forty two, the words,
this gets to be the string forty two.
It runs here, this blows up.
So it comes and runs this part here.
And then it says, if ival is greater than 0, which is not true,
so it runs this part and says Not a number.
So this is our way of compensating for user input
that might have errors in it, errors that we're anticipating, right?
You'd, you'd rather at least put up some kind of a
message, rather than just have a traceback,
if you're writing code for somebody else.
It just kind of is
not very classy. So,
the classic program to do this is a time-and-a-half for overtime pay.
So you get some pay rate like $10 an hour for your first 40 hours,
and then you get 15 hours, for any hours above it.
So you have to sort of say, oh, okay, if this ends up being, this ends up being
some kind of a thing, where, let me draw that picture a little better.
Hours greater than 40, you're going to do one thing, and
if hours are less than 40, you're going to do another thing.
So you have two payout calculations.
If the hours are greater than 40, then you're going to do
a overtime calculation, which is kind of like, 40 times the regular rate.
And then, the number of excess hours, like 5 overtime hours
times the reg, rate times one-and-a-half.
So this is kind of the calculation that happens if the hours are greater than 40.
And then, if the hours are less than 40, it's just pay
equals rate times hours.
So it, you're going to do one of two calculations, depending on how it works.
So that's one of the programming problems for this chapter.
That's a classic.
It's the classic if, then, else.
You can actually do it with an if, then if you're tricky.
There's a lot of ways to do this, so pick a, pick one and do it.
Now the next thing I want you to do is, I want you to take that
same program, do it again in another, another assignment, or another problem in
the chapter, and that is have some kind of a non-numeric input, and have it blow up.
So if they type, you know, something like nine, put out an error.
Or, if they type something here, put out an error.
Now, don't write a loop. No loop.
This is one execution of the program, and this is another execution of the program.
Later, we can write loops.
We haven't even talked about loops. So this is running it twice.
All I want you to do is exit.
So take a look in the book as to how to just get out.
So it, it's, it, I don't want you to try to say, I'm going to
prompt for these numbers until I get a good one, we'll do that later.
I just want you to deal with the fact that
you read a thing, you use the try
to convert it to a float and see if it works.
And if you don't, you just quit.
Don't, don't, don't try to be tricky and repeatedly prompt.
So don't repeatedly prompt. One prompt, and then
quit, okay? So, this is contintous-,
conditional execution, if, if then else, and then I added a little bit with
the try and except, as well.
And the try and except is really a limited kind of a problem.
It really is to compensate for errors that you anticipate are going to happen, and
you can imagine what you want to do
as a replacement for what those errors are, okay?
See you next lecture.