Hello, and welcome to Chapter Three of Python for Informatics.
Chapter One, Chapter Two, now we're
going to get to something kind of programmy.
I mean, assignment statements and reserved words, that just kind of gurgling.
Now we're going to start seeing composition.
We're going to start seeing the conditional execution.
Gets us started, sort of, seeing the power
of computers, where you're starting to make decisions.
So, as always, this lecture and audio, video, and slides are also available,
are copyright, Creative Commons Attribution.
So, conditional steps are steps that may or may not be executed.
So here's, here's a bit of code.
So, and, and I draw these pictures. I won't
draw too many of these pictures on the left-hand side.
If you've taken a programming class, you may have seen these.
They're sometimes called flow charts.
Sometimes people really think these are important.
I don't think they're all that important for understanding.
The, the Python code is here on the
right-hand side, and this picture's on the left-hand side.
And, and the reality is is that this may, initially, make more sense, cognitively,
to you, than this. But this part on the right-hand side is what's important.
I like to call these like road maps, so you can sort of
trace where the code is going by driving down a little road.
That's kind of a, something that you do once or
twice and then, pretty soon, you'll start reading the code.
So I'm going to start on the right-hand
side here, and just walk through the code.
Remember, code operates in sequence.
Well, there is a if, which is a special reserved word.
It's one of those things that you can't, you can't name a variable if.
And it is our indication that to Python, that the next
statement that we're going to do may or may not be executed, if.
And the thing that comes on the same line as the if,
up to and including the, the little colon, is a question.
This is a question.
You're asking a question.
So an assignment statement is moving a value into a variable.
And a if statement
is asking a question.
In this case, we're asking a question about a variable.
So always think, when you're, sort of, here, that this is a question to be asked.
And, you'll notice when I'm doing the same
thing over here, I put a question mark there.
Is x less than 10? Yes or no?
It's a question that has a yes or no.
And so, the way this works is, this statement
that's indented, after the if, is either executed or
not executed based on the result of that question.
So the way to sort of read this in English is set x to 5.
If x is less than 10, which it is because x is 5, then we're going to execute this.
So print Smaller comes out.
And then we come back out and we continue and say, oh, okay, now
I have another if statement, and then a bit of, a block of indented code.
If x is less than 20, that's the question.
The answer to that
is no, and so it does not run that line, and so it runs Finis.
So the printout of this program is Smaller, followed by Finis.
What happens is, this line never executes because the answer
to this question is false. Okay? So, let's go through that a little faster.
Set x to five. If x is less than 10, print Smaller.
Then, if x is greater
than 20, which it's not, skip that, and then print Finis.
That's the short version of it, okay?
Conditional steps.
This step is conditional, this step is conditional.
They may or may not be executed based on the result of the question.
Now, if we're thinking of this as like a GPS
road map or something, we can look at this right-hand side.
So, the CPU comes roaring down here, x equals 5, okay, I'll run that.
Then it's faced with a choice. Do, is x less than 10, yes or no?
If it is yes, and it is, I will go this way.
If it was no, I would go that way.
So if it's yes, I go here and I run this little thing and I print Smaller, great.
And I follow the little road.
And now the road takes me to here. And it's asking another question.
Is x greater than 20?
This time, the answer is no, so I'd come down here, right?
And so,
this bit of code is never executed.
Now, this is a very simple example, but you get the basic idea.
Okay? So that's conditional execution.
Now there's a number of conditional operators that we
want to use, just like we had multiplication, division.
Some of them are pretty intuitive, and the others, you just kind of have
to memorize, like less than and greater than make a lot of sense.
The one that probably, that, easy, like less than or equal to,
or greater than or equal to, those kind of make sense, too.
They're less than or equal to.
Just because we don't have a less than or equal to sign on a
symbol or a greater than or equal sign, which we would use in mathematics.
Equality, asking the question of whether something is
equal to something else or not, is double equal.
And that's because we're already using single equals as assignment.
So when we say x equals 3, that is an assignment and sticks a value into x.
This is the question.
Is x equal to?
If I was building a language, I would make
it be equal question mark, or something like that.
I'd be like, huh, is it equal? Kind of a question mark.
But that's not what we do.
I didn't invent this, so we're, double equals
is the question, is something equal to another.
Single equals changes something, x equals five changes x.
Okay, and then, not equal, exclamation is commonly used to mean not in
computer contexts, so if something is not equal to something,
it is exclamation equal. Here are some examples.
Just kind of running through them.
They're all, they all turn out to be true, because I set x to 5.
If x equals 5, print Equals 5.
Come out here, if x is greater than 4, which is true, print Greater than 4.
If x greater than or equal to 5, yep.
If x less than 6, print Less than 6.
Now here's a, there are two, sort of, syntaxes to, to the if statement.
One is where the if statement is down here on a separate line and it's indented,
and the other is where there's a single line and it's right on the same line,
if x less than 6, print Less than 6.
So this is true, so this whole thing executes.
Then it continues down, if x less than or equal to 5?
Yep, print Less than or Equal 5.
If x is not equal to 6, which is true, cuz it's 5, then Not equal to 6.
So, all those will turn out to be true, and all those will execute.
And so, the, the tricky bit, here, is, you know, just knowing,
seeing this syntax for an if statement, where it's all one line, and this syntax,
where you end the first line with a colon and then indent the second line.
This, you can only do one line.
We will soon see that you can put more than one line in the indented block.
Okay.
Here we have more than one int line in
the indented block, these are called one-way decisions.
And so, we say x equals 5,
we print out Before 5, so that prints out.
If x equals 5, remember the double equals is the question mark version of equality,
single equals assignment, it says yes.
So we indent, and the convention is to indent four spaces,
although it doesn't really matter as long as you're consistent.
Then it's going to run all three of those.
Is 5, Still 5, Third 5, these lines all come out.
And then it comes out and prints,
and the de-indenting, the fact that this print has been moved to line up with
the if, that's what indicates that this little
block of conditional executed code is finished.
So then prints out Afterwards 5, comes down some more, Before 6, then it asks
another question, if x is equal to 6,
again, that's the question mark version of it.
And if this is false, now, because x
happens to be 5, so the answer
to this expression, the logical expression, is false.
Then it skips all of the indented bits, so none of this executes.
So, since it's false, it skips all of the indented bit, but then it,
this print lines up, and so then it picks back up with Afterwards 6.
So we call this a one-way decision, where you have the question, and then
you have a couple of things that you're going to do on this true, true thing.
Or, if it turns out that you're false,
you're going to skip all those things.
So, Python is actually one of the
few languages that uses indentation as syntactically significant.
We like to indent code to, for ifs, and
in a moment, we'll see you learn about loops.
We like to indent code as a way to
make sense of stuff, it makes it easier to read.
You know, if this thing's inside, and so, it, it's really quite nice.
And then,
we, sort of, use it as a matching, to
help us cognitively understand what's inside of a program.
But in Python, it's really, really important, and it's
almost, it's, it's, you have to think of, like,
when you are moving in, you mean something,
and when you move back out, you mean something.
So you can increase the indent, which you do after, like,
an if statement, or any other statement that ends in a colon.
You increase the indent, and then
when you're done, you decrease the indent.
You maintain the indent, sort of, for sequential code.
Now blank lines and comments are ignored.
So you can have a blank line and it, it, the
indentation just goes right past it and the comments don't affect it.
And so, while we're here, we'll interrupt us for a recommendation.
In your text editor, Notepad Plus or Text Edit or TextWrangler, or whatever
you're using, it may be set, when you hit the tab key, to move in four spaces.
Sometimes you also might move in four spaces by hitting space bar four times.
Python will see that as different.
And it is possible in all of these word processors to say, hey,
don't actually put tabs in my document, when I hit the tab, put in four spaces.
Then, whether you're hitting the space bar or hitting the tab, at least you
are putting the same thing into your
document and don't, not freaking Python out.
If you don't, you may get indentation errors.
Indentation errors are syntax errors to Python.
And what's really frustrating is, if you, it looks good
to you in your text editor, you have an if,
and the block goes in, and comes back out, but one of them is
four spaces and one of them is a tab, then Python will yell at you.
And this is really frustrating, when Python yells at you about that.
So what I'd like you to do is go into your text editor, whatever it is,
into the properties or the settings.
And here is, you know, your, yours may be different, but here is where you set this.
Auto expand tabs, that is on the Mac in TextWrangler, and then,
in Notepad Plus Plus, there is replace
tabs as spaces, and that's underneath preferences.
So you have to find it.
Stop right now, and go set this so you're not going to make yourself crazy.
Okay, so, this is kind of a busy slide, but it gives
you the sense that you have to explicitly think about indenting and de-indenting.
Okay? And so I'm just going to walk through this.
So, when you have two lines lining up
that means they're going to run sequentially.
If you see an if, or later here, we'll see a for.
We haven't talked about for yet, but it's, it's like if.
So, the fact that we go from this second line
to this third line and move the indent in, we're actually
creating a block that has to do with this if,
and it, you can also kind of tell these, the if and
the for end in a colon character.
Now, we could pull this print back out, but we want
it to be part of the if, so we maintain the indent.
And then we're done with the if by pulling out.
So we line the p with the i, and that means this is outside of the if.
This for, which we haven't learned about for yet, for is another
statement that ends in a colon, and afterwards you have to indent.
Then you maintain the indent. Here's an if.
But now we have an if, and we're already in,
but that ends in a colon, so we go in farther.
And now this is the block.
Now, we come back out, and we line up with that if, right there, okay?
And now, at the end of this, this indent, this x here
comes all the way back out, so it lines up.
The rest of these are kind of weird in that
comments don't matter, blank lines don't matter.
And so, it just is, sort of, you have to
get, mentally get used to the notion that these don't count.
They can really cognitively mess you up.
So these don't count.
And now, if I look through it, without,
with the comments hidden, it starts in column one.
Ignore, ignore, goes in, stays in, ignore, ignore,
ignore, comes out. So that's, it all makes sense.
Those comments and blank lines are just, kind of, confusion.
So, increasing and decreasing indent has meaning in Python.
We'll learn more about this in a bit.
Our programs won't get this complex right away, but it's important to
think, these indents aren't just pretty;
they actually are communicating something to Python.
And what they're communicating is, basically, what's in a block.
And it shouldn't take you very long, when you
start looking at Python, to sort of visualize these blocks.
So, here, here's a big block.
This block here, that's got these three things.
And then, this is a block as well, and
you can kind of say, well, here's an if statement.
And then these are the two statements that are part of that if statement.
So mentally, you kind of make these block pictures.
So here's another block. This is that for loop.
This part's the indented part, but then there's a block inside of the block.
So you gotta kind of start seeing that as well.
So this is a block that has to do with this green block
is the, the one that has to do with the if.
And then there's a block here, and then this is
a great big block because this is where it finally de-indents.
So, don't worry about it yet, but at some
point you're just going to start seeing this indenting and de-indenting
as defining blocks of code for various purposes.
Now we don't have all the purposes yet, but we'll get there.
So, we saw in that previous thing one block within a block.
And, and we're going to do that.
We can have ifs, we can have loops that
get indented, but then we can indent even more.
We call that nested, where there's an indented
area that's in an area that's already indented.
So here's a nested decision.
And it might be easier to start on
this side, where I'm going to have a first choice.
Is x greater than 1, yes or no, and if
it's yes, I'll do some work, and then I'm going to
ask another question, and if that's yes, then I'm going to
do this, then I'm going to come all the way back in.
And the way we encode this in Python is, x equals 42,
if x is greater than 1, it's true, so we continue working in the indent.
And now we say, oh, if x is less than 100, which is still true,
so we go in farther, and we do this, and now we come out.
We don't come out to here, we actually keep going
all the way to here, so that ends both blocks.
And so if you sort of think about this,
again this is where I want you to start seeing
what's in a block of code and what's not in
a block of code, and how the indents sort of,
like, put a boundary on the blocks of code.
And so, the first thing you should see is, sort of, like, that
purple part, the, the x less than 100, print, that's kind of a box.
And you can see the box on the, on the sort of flow diagram as well.
The boxes are there. The boxes on the flow
diagram are places where there's one entrance and one exit.
And then there's also, sort of, the larger box, right?
There's this if box that includes the smaller box.
So, the, there's this nesting, which is boxes
within boxes, or indented areas within indented areas.
Now that was a, what we call a one-way decision, where
you're doing if, and this code either runs or it doesn't run.
It is extremely common to want to basically say,
look, I'm going to do one of two things. I'm going to
ask a question, if the question is true, I'm going to do
one thing. If the question's false, I'm going to do another thing.
So that's what we have shown here.
We say, is x equals 4, is x equal to question mark?
If it's yes, we're going to go here.
If it's no, we're going to go here.
We,re going to execute one or the other, and then we're going to continue.
So we're really at a fork in the road here, right?
We're, we're at a fork in the road, going to make
a choice, and one or the other, but never both, right?
So, we're going to do one thing, or we're going to do another thing.
We're going to do one of the two, and
depending on what the question that we ask,
the question that we ask is, which one that we're going to do.
So here's a little bit of code.
x equals 4, is x greater than 2, the answer is yes.
Then we come out and hit this else and we automatically
skip, right, because we're only going to do one of the two.
And here's the picture, x equals 4. Is x equal to yes?
Print, done.
Which means we'll never do both this and that, never do both, both sides.
We're going to do one or the other of the sides.
And just sort of going with the box,
that is our box, oops, go back, go back.
This is our box, right?
It's sort of the indent followed by the final indent.
The else is really kind of part of it.
And then we can draw the picture here. It has one entry and one exit.
Okay. So we have one-way ifs, and we have
two-way ifs, and now we have multi-way ifs, okay?
So, here is a multi-way if,
and it introduces a new reserved word, elif,
which is a combination of else and if.
And this one, probably, is just as easy to talk about the picture here.
The first question is asked, there's still
going to only be one, there's only going to
be one, one and only one of these three choices are going to run.
Once it's run one, then it's done, okay?
So the, the way to think about this, if x is less than 2, we're going to
run this one, and then we're going to kind of
flush all the way out to the bottom.
If x is not less than 2, and it's less than 10,
we're going to run this one, then flush out the bottom.
And if x is not less than 2, and x is not
less than 10, we're going to run this one, and flush out the bottom.
So, one of these three, one, two, three, one of those three is going to run.
And it's going to run based on the questions that are being asked.
The questions do get asked
in an order, and the order does matter, okay?
So that is a multi-way if.
If, else if, else. So this is kind of like an otherwise.
The else is like an otherwise, you know, one way or another, we're going to run
something, and if none of these first two have run, we will run this one.
We call it a multi-way if, okay?
So, here's an example of our multi-way if.
That, if we say x equals 0, x equals 0.
Is it less than 2?
Yes, it is.
So we run Small, print Small, and then we flush out the bottom.
If we switch, instead, x to 5, x is 5.
Is it less than 2? No, it is not less than 2.
Is it less than 10?
Well, 5 is less than 10. So the answer is yes, so we print Medium,
then we flush out the bottom. One and only one are going to execute.
Now, in this case, we got x is 20.
And so we come through here. Is it less than 2?
No, it is not.
Is it less than 10? No, it is not.
So we're going to do this one, and then flush out the bottom.
If we go here, it's false, false, go here, all else being equal, we run that one.
So this one doesn't run
and that one doesn't run, right? Because these are like gateways.
If it were true, it would run it.
But it's false, so we're going to skip it.
This one, it's false, so we're going to skip it.
But then we hit the else, that's like a catch-all.
And then if none of these were true, then it will run the else.
Any questions?
Okay. Well,
I'm going to ask you a question, in a second.
Okay, so just a couple of things that probably you're wondering about.
You don't actually need an else.
You can have a multi-way, x equals 5, if x is less than 2, there's no else here.
You'll notice that this print just comes back.
And so this way, it could, if both of these are false, it could
skip them both and just run right through here, and there's no else clause, okay?
So, in this case, if this one's
going to, the way this one's going to run is, x equals 5
if x is less than 2, it's, it's not.
And it skips to here.
Else if x is less than 10, which it is, it will run that one and come here.
But, for a different value of x, like 95, boop boop.
If x was 95, or 59, this would be false. It would skip it.
This would, elif, would still be false, and it would skip it,
and the only thing it would print out would be, All done. Okay?
Okay, you can also have many elifs.
So, better change to green.
It checks this one, if it's true, it runs the first one.
If it's false, it checks this one.
If that's true, it run this one, and then it skips, right?
And so, so the way to think about
this is, is, it just goes through and checks this one
false, this one false, false, false, oh, I finally found one.
And now I'm done.
It still is going to do one and only one of these.
This one has an else, so that sooner or later, it is going to do one.
And it only will do the else if all of these are false. All have to be false.
Then it will, actually, come and hit the else clause.
It's great, because there are lots of situations where
you're like, oh, is it before eight in the morning?
Or is it between eight and noon?
Or is it between noon and five?
Or after five? After midnight? I don't know.
Okay, so, here, coming up, is a question.
And, you, there's two puzzles and I'm going to
stop so you can look at them for a while.
And I want you to figure out,
in both sides of this, which of the lines
will not execute, regardless of the value for x.
So in both sides, there is a line
that won't execute, regardless of the value for x.
Which will never print?
There's two problems, problem A and problem B.
Okay, I'll have some coffee while you think.
[NOISE].
Okay, hopefully you paused it so that you could actually think for a bit.
So, so I'm going to guess you probably
got the first one right, that's pretty straightforward.
I mean, actually, you're in great shape if you got both of them right.
If you got any of them right, you're in great shape
because that means you're starting to get it.
Starting to like, oh, I'm seeing, kind of, this flow picture, there's a picture.
I look at these characters that seemingly
look like gibberish, and a picture arrives.
Or a pattern of access execution arises.
That's what we want to see.
So, the in the first one, which will never print?
Well, we're looking for kind of a value for x which will be defective.
So if x is less than 2, we're going to do this.
Else, if x is greater than or equal to 2, we'll do this, else we'll do that.
Well, here's the problem with this one.
For all values of x, it is, is either going,
x is less than 2 is either going to be true or greater than equal to 2.
Greater than or equal to be, pah, for X to be
greater than or equal to 2 is going to be true.
So it's going to run this one, or it's going to run that one.
So for big numbers, numbers above 2,
it's going to run this one; below 2, it's going to run that one.
So this one is never going to run, okay?
Because the, one of the first two is going to be
true, and so the third else situation is not going to run.
Hope you got that right.
Okay, so let's take a look at the next one, okay?
So the question is, you know, if x is less than 2, do this,
if x is less 20, do that, and if x is less than 10, do this,
and otherwise do that. Well, the one that will never execute
is this one. And, x equals 15,
no, x equals 15 is a bad one, x equals 5 is the one that will,
sort of, cause it to behave badly.
And so, if x is 5, this is false.
If x is less than 20, this is true, and then it's done.
So the problem is, this is the one that will never execute, because
if a value is less than 10, it's also less than 20, so this will be true.
So for a value like 5, which happens to be less than 10,
which you would think would cause that line to execute, does not.
This one executes because it's checked first.
Now, if we just moved this code, took this code and
we moved it down here, then it would make more sense, okay?
Oops.
If we moved it down there, it would make more sense.
But basically, the answer to these is, change color, this one won't
execute, and this one will never execute for any value, so there's the answer.
Okay, so we're almost done with conditionals.
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.