Hello, and welcome to a worked
exercise for Python for Informatics: Exploring information.
I am your host, Charles Severance, and
this material is copyright Creative Commons Attribution.
So again, you're supposed to try to
work these exercises yourself before you attempt them.
Just, these are good crutches to make sure that you
understand, but make sure you give it a try yourself.
And if you don't understand how to basically
create and edit programs, you should go back
to www.pythonlearn.com, and work through some of the
installation instructions for Python and your text editor.
So here's our exercise, we're going to do the classic rate times pay with overtime.
And so the idea is that, oops, you have, pay times rate and for 40 hours you
get the basic rate and if you work above it, you get one and a half times that.
So if it's $10 an hour, you get $15 an hour for the hours above 40.
So let's go ahead and start TextWrangler, and start
a command line. I'm going to close that, close our drawer, make it smaller.
And I'm going to, I'm going to pull my, go into the
Desktop, go to py4inf, see I've got a few files here.
And in this case, I am going to open hours1.py.
And I'm going to immediately save it as hours2.py.
I mean, why rewrite all that stuff we did before?
So this I guess I can get rid of this one now.
So it's hours2.py.
And so now we have, the, I'll keep that in.
We have the rate and the hours.
I'll put that print statement back in.
And the problem is I can't
I can't just do pay equals rate times hours, so I need an if-then-else.
So let's do an if.
If the hours is less than 40.
And we can say, pay equals rate times hours, that's fine.
So now let's come up.
I'll make that four spaces.
There we go.
I could write another statement that says like this.
If, actually it should be less than or equal to 40.
If pay is greater than 40, we need a different calculation.
And, and that one is pay,
is, there's a couple of ways to do this, but I'm going to do it this way.
Rate times 40, which means those are the, those are the hours.
That's your basic rate.
Then I'm going to add rate times 1.5, that's one and a half times
the rate, times, and then the hours above 40, which is hours minus 40.
So this bit here is the hours above 40.
Now operator precedence would say that
these multiplications are done before this subtraction.
And that might not work so well, so let's
go ahead and run this and see what happens, okay?
So we got pay is rate times hours, and pay
is equal to rate times 1.5 times hours minus 40.
Let's go ahead and run this.
I got python, hours.
[BLANK_AUDIO]
Let's do 30 hours and $10, that's looking good.
So, that is, we have, you know, and
that's running through this bit of code right here.
And let's do exactly 40 hours, and that's looking good.
So let's do 50 hours because we can
calculate that in our head, that's 10 hours extra.
And then a rate of $10 should be 500.
That doesn't seem right, so 50 times 10, what's wrong here?
50 times 10?
[BLANK_AUDIO]
Hours is less than 40.
[BLANK_AUDIO]
Now I'm confused.
Let's run this, well, oops.
What did I do here?
Pay is greater than 40, that does not seem right.
Did I save this file?
I'm crazy.
50 hours, $10 an hour, oh, okay, I must not have saved the file, okay.
So, [LAUGH].
Pay is not defined on line eight.
Oops, I was typing too fast.
That needs to be hours greater than 40.
Okay.
So that's better, so now we have hours less than 40 and hours greater than 40.
So let's run that one again.
50 hours at $10 an hour.
That'd be like 600 something, this does not look like the right number.
[BLANK_AUDIO]
We, and so we got I will, I will just take
this print back out, print of rate and hour, save that.
Let's run it again.
50 hours, $10 per hour, $1100.
That does not look right.
Let's take a look at this.
Rate times 40, that's pretty obvious, it'd be $400.
Rate times one and a half, that'd be $15, times number of hours minus 40.
Oh, so this is an operator precedence problem.
So, if you recall, multiplication happens
before addition, and so Python is going to do this calculation first.
This is the actual calculation, it's multiplying the rate times one and
a half, times the number of hours, and then it's subtracting 40.
This ends up being a rather large number, and that's how our calculation is wrong.
That is not how we meant to do this.
Oops, so let's put parentheses in, because we want to subtract 40 from
the hours before we do the multiplication. We want this to happen first.
We want the hours to subtract the 40 so we'll end up
with 10 hours and then multiply that by the rate.
So let me save that.
And run it. 50 hours, $10 an hour.
550.
Much better, much better.
[WHISTLE] That looks pretty much better.
Okay, so that's, so we're kind of done with that.
We had that little mistake there, of the parentheses not working.
And the other thing is, is that the much better
way to write this is just putting an else here.
Alright?
So we just say else.
And so now, we have an if-then-else.
If it's less than 40, less than or equal to 40, we do this.
If it's greater than 40, we do that.
And then we do print pay.
So let's save that one.
So 50 hours, $10 an hour, 550.
So this if-then-else formulation is a
much better way to do this particular problem.