Hello, and welcome to the worked
exercise for Python For Informatics, Exploring Information.
I'm Charles Severance, and I'm your guide
and guest programmer for this particular example.
As always, this material is copyright Creative Commons Attribution.
So we've been working through a series of
exercises that are doing time-and-a-half for overtime and
pay of various kinds to look at various
if-then-else, and now we're on to functions.
And so, we're going to create a
function called computepay that takes two parameters,
hour and rate, and it actually does exactly the same thing as the other one.
It's just we're doing it in a different way.
Okay?
So, we will start.
[BLANK_AUDIO]
I'm running TextWrangler and the Terminal program.
[BLANK_AUDIO]
Make the Terminal program smaller and move it to the bottom.
TextWrangler smaller and let's go
cd desktop, cd python for informatics,
and I've got some files here. Let's let's make use of hours2.py.
So, I'm going to open hours2.py.
Hours3.py was my, or actually, no, I'll use,
I'll use hours3.py because that's my try-accept code.
Okay, so the idea is we're supposed to encapsulate
this bit of code in a function called computepay.
So one of the things you do with functions
is you have to define them before you use them.
So we're going to put the function up here and use the def keyword,
def computepay, and we're going to accept some parameters.
I'm going to call this, I'm going to call it h and r
just to kind of emphasize the difference between the parameters we use inside the
function and the arguments in the call.
So the code that needs to be in this
in this function is these lines of code right here.
So we're going to to cut them out of here, and then
we're going to indent them, make them the body of the function.
[BLANK_AUDIO]
Okay, so, now the variables in this function are not h, are not
hours and rate, they're p, I'm going to just change them all to this.
This, I'm just changing this to
kind of emphasize the difference between these variables
that live in the function and the variables that live in the calling code.
So in the function they're called h and r and p, and the only one other thing I'm
going to do is I'm going to put a return statement in
to return p as the result of the function.
And so Python will come in, it'll see the def,
it'll read all this stuff, it won't do it.
Then the first executable says try, it still works the same way,
and I'm going to change this to be In the main code.
Rate and hours.
Then I'm going to put a print statement just for yuckydoodles.
print In computepay,
and print out h and r.
And then before I do my return here, I'm going to say
[SOUND] And I'll print out, I'm going to print out
what I'm sending back and print out the computed pay.
And then, now the only thing I've got to do left is,
I've got the rate and the hours, and I'm going to call my function.
So, I'm going to say pay equals computepay, or invoke the function,
and pass in rate and hours as my arguments.
And then print it out.
So here's the main code.
Oop, well, I'll just say We are back, and then print out pay.
So I have a print before and after the call to computepay.
And I have a print once computepay starts and when computepay finishes.
I have to sort of reorganize my screen a little bit here.
[BLANK_AUDIO]
So we can see them all.
Ooh, I gotta save this as something else so I don't ruin hours2.
I'll call this hours4.py.
Now I'll say python hours4.py.
[INAUDIBLE]
[BLANK_AUDIO]
Okay, [COUGH] so I'll type, so what's happened here is it's typing
Enter Hours, so it's on this line here, line eleven.
It's already made it all the way through here, okay?
Now I'll have a print statement in a second here.
So, so, so, there we go.
It's made it through, it hasn't done any work but it remembered
this code and stored it under the name computepay and it's here.
So now I'll do hours.
40 hours and $10.
So I'm running through this code right here.
And then I'm going here next.
Because we're not going to bad, put bad hours in.
So the next line that happened is In the main code, which is here.
Then it called computepay, right there,
so it got here, and h is 10 and r is 40.
It doesn't quite look right, but the computepay, we're done,
and that's 400, and everything is cool.
So it's back, right?
[BLANK_AUDIO]
So, let's do this.
Let's do another one.
And I'm putting 50 hours and $10 an hour.
Now that one should have overtime, because we put in 50 hours, but it's not.
So what is the problem here?
Ten and 50 are coming in.
We, we're getting the numbers right.
We're going into computepay as 10 and 50, and then we're coming back.
Hmm.
So, we're printing out the hours, the h and the r are variable.
[BLANK_AUDIO]
Oh, ooh, ooh, ooh, ooh, I see it, I see it.
Mistake is right here and right here.
Can you see it?
We have, these are parameters, and these are arguments.
These are the real variables, rate and hours.
The parameters are h and r.
We got two parameters, but we've got the order wrong.
[BLANK_AUDIO]
So, here I have rate first, so h here is rate.
And look, if we look at what we print out here in computepay, the first
thing we print out is h, but it's really 10 which is supposed to be rate.
Oops, so I gotta fix this.
That should be r to correspond to the first parameter,
and that should be h to correspond to the second parameter.
And just to keep my sanity, I'm going to print out r comma h,
so I'll say rate, hours just to be consistent.
Actually no, no, no, no.
Since I'm prompting for hours and rate, h comma r.
I'll change this to h comma r, and then make this be
hours comma rate. I think it makes more sense for the hours to be first.
I don't know why that makes more sense,
but now we're corresponding our mnemonic variables inside of our functions,
our h and r, that allude to hours and rate.
But they're not the same as hours and rate, so
h is an alias to hours when this is called.
Okay?
So now let's run it again and see if she works.
So I'll do 50 hours, $10 an hour.
Oh, I'll bet I didn't save it.
[MUSIC]
50 hours, $10 an hour, yay it works,
it works, it works, it works, we've got ourselves our overtime.
Okay, so, so I'm going to say h equals.
Then I'm going to add r equals here, just so that we know
which rate and what h and r are.
Just add some strings there.
[COUGH] Run it again.
If I'd have done that in the first place, I
probably would not have made that, seen that mistake much faster.
So, we've got 50 hours and $10 per hour, and so it's working.
So just to sort of continue on here on this, I could do this.
I could call this function again, and I could say, just to show you how I'm
reusing this code, I'm going to make a variable named z,
And I'm going to say 12.2 is my parameter. And my second parameter is $10 an hour.
And so now I'm using constants instead of variables and you will see that it's
going to call computepay once this time and h and r are going to be hours
and rate the first time, and the second time they're going to be called again
but 12 and 10 are going to be what h and r are the second time.
And then print ZZZZ, comma z.
That's just a silly message.
Okay?
So, we're going to, this, this stuff is unchanged,
it just prompts and gets us the rate.
We're going to call computepay twice this time.
One time with hours and rate, and the second time with 12.2 and 10.0.
So lets take a look at how that runs.
Right, may, may need a little longer, well let's see, 50 hours, $10 an hour.
Oh yeah, it would help for me to save, help for me to save.
Don't make that mistake.
50 hours, $10 an hour.
Okay. So In the main code, then it
then it calls computepay and does one computation
and then it's here, it's back in the main code,
right there, and then it calls computepay again.
This time the variables h and r, oops, this times the variables the h and r
are 12.2 and 10.0. The last time, h and r were 50 and 10.
Okay?
The key is that this h and r, the parameters in the function, are ephemeral.
They come and they go.
computepay works with different, different values each time.
But it uses h and r internally as kind of its scratch area for this.
Let me see if I can get this all on one screen. Yes I can.
Okay, so this just is showing we're running it twice
once h is 50 and the next time it's 12.
Because it's simply the first parameter.
And the first time the parameter was 50, the second time it was 12.2.
Okay?
So, let's just walk through,
scribbling what's going on here.
Just, we'll just kind of go through this.
So Python starts at the beginning and sees def, and it says
oh, don't run this code, remember it, name it computepay.
So h and r don't exist, they're not, they don't exist until the function is invoked.
Then we get to the try.
We've got some insurance on these four lines.
We read our, and convert our input, and our rate, our hours
and our rate, and that's good, so we don't run the except.
Then we print In the main code.
Okay?
And then we come to this line and we see computepay, and so that
goes up here [SOUND] and h and r are hours and rate and it runs this code.
It returns p, and that p comes back as the value here and ends up being put into pay.
Then we print We are back.
Then we run into this statement and we have to go call computepay
so it goes up again and this time h and r are 12 and 10.
It runs through the code, blah, blah, blah, blah, blah, and then
returns 122 and that returns to this call, not the previous one.
And then that puts 122 into z. And then the program continues on.
So, it, again this does nothing and it calls it twice, and has two sets
of arguments and the parameters represent different arguments in the different calls.
Okay?
So let me just clean this up one last time and
get rid of the, oops, get rid of the debug print statements.
Get rid of that.
Get rid of this.
So you can see what it's really supposed to look like.
We don't need that.
Well, I'll just comment that one out to keep it in case of later.
This is debug print, so I'll keep that and get rid of those two.
So now it doesn't do any printing.
So let's run it one more time.
40, 10, good stuff.
50, 10, good stuff.
So, it works.
So, there's the, there's the ultimate program and it looks pretty good.