Hello and welcome to the, a worked
exercise for Python for Informatics, Exploring Information.
My name is Dr. Charles Severance, and
I'm your host for this particular worked exercise.
This material is copyright Creative Commons Attribution.
Again, try and work these exercises yourself.
In the beginning they're intended to be easy, they're
intended to be worked from the book and the other materials.
Not, and this is supposed to be used as a last resort.
But you're here, so I assume you know those rules.
I assume that you also have everything installed.
Python's installed, you know how to use your text editor,
you know how to use your command line.
This
screencast will not, sort of, go through all of those.
Although it will review some of them.
Go back to pythonlearn.com and go through the software installation screencasts
to get to the point where you're ready to run programs.
So here's our exercise.
We're going to write a program
that's going to enter the hours and enter the rate and then compute pay.
It's going to prompt and read some strings and convert
from floating point and then do some multiplication, and away we go.
So, I'm going to minimize that. I will start up my TextWrangler.
And this is the last program I was working on.
[BLANK_AUDIO]
And I'll hide those, get rid of those.
Start up my Terminal, make it a little smaller.
Pull it down here and TextWrangler.
So now I have a new document.
And so I'm going to do what I always do and so the first thing we've got to
do is we've got to prompt for some hours.
And so I'm going to make a variable called inp
which is just sort of mnemonic for some
input, it's a string, it's raw input, always, always
reads a string, it doesn't read it out, even
though they're numbers, it reads them as a string.
We'll have to convert them.
raw_input [SOUND] Enter hours
colon space.
And I'm just going to keep it simple, and I'm going to print inp out.
Now I have to save this file, Save As, and I'll put in Python here, and I'm going to
call it hours1.py, because we're going to do a whole series of these things.
Save that.
Now you'll notice that as soon as it saves as a .py then it does syntax coloring.
Thank you very much.
So then I go into my command window, cd Desktop, I have
everything in this folder py4inf on my desktop, right there, that folder.
cd [INAUDIBLE]. Then I do a ls.
Again, dir for Unix, and then, here we go.
So I say python h-o, hours1.py
again, and I, I use tab a lot, because I just don't like typing.
So let's run it.
So I enter some hours, 123, and it prints out 123.
And just to show you it's just a string, I'll just put
some crap in there and out comes exactly as you'd expect, crap.
So it's not really.
And let's do this.
Let's just say, print type of inp just to sort of prove ourselves.
It's a string, right?
It is a string.
It is not an integer.
So how do we convert from a string to an integer? Aah, float.
We use the built-in function float.
And now I'm going to call my variable hours to be quite mnemonic
and I'll call the float conversion
and then I'll print hours and print the type of hours.
Now you might say, hey Chuck, you know, you're a pretty smart guy.
This is only like five lines of code.
How come you're doing this slowly?
How come you take steps? That's what we call interative development.
So I'm just being really safe here.
I'll get to the whole program in a bit.
I mean, that's this dot, dot, dot stuff down here.
There'll be more.
But I'm, I'm just sort of playing with it one bit at
a time and, and making sure that I know what's going on.
And even, even in, I don't, I'm doing this a little bit more exaggerated here,
but when I write programs, I usually start with hello world and then add to it.
It's, it's sort of the, you start with sort of a nice
blank canvas, and put some basic stuff on the back of it. Okay.
So, so now I'm going to read some input.
I'm going to, I have to prove that this is a string.
I'm going to convert the string to a floating point, put in a different number.
Then I'll print out that thing and the type of it
just to sort be sure that it works.
So I have python hours1.py and I go 1, 2, 3.
And it turns out that now I, I've converted it to
1, 2, 3, 4 and I've got myself a float.
And of course now, if I call, if I put junk in there, it's going
to get us a traceback error, because it just cannot convert dskj to a float.
But we're not going to, we're going to fix that later.
Okay? So now I'm going to start deleting some
of this stuff, because that's pretty clear that that's working.
Okay?
So now, I've got to prompt for rate.
Well, good programmers don't type if they don't have to.
So I'm just going to cut and paste this and then I'm going to change this to rate.
So raw_input Enter Rate. And then that will be
a string of course, then I will change that to a
float and I'm going to put that into rate.
And just to be sure, I'm going to print out rate, hours.
Now at this point, they should be floating point
numbers, so let's run that and we'll start with a
simple test, 40 hours, 10 dollars an hour.
There we go, they've been converted to floats, just peachy fine.
So I'm going to create a variable called pay, rate times hours
and we'll print out pay. And I will just get rid of this one with a comment.
And did you notice how I used a comment,
a pound sign, there, because, I'll, I'm, I'm not deleting that.
I'll keep that in case something goes wrong.
I don't think it's going to, but
here we go.
Whoa, syntax error.
Invalid syntax, pay rate times hours.
And oh, I forgot the equals sign.
Need to make that an assignment statement.
Let's try this again.
python hours1, 40, 10, 400.
There we go. And let me go and check.
Let's run the data.
35 and 275.
I'll run my program again.
35, $2.75 an hour and 6.23.
Is my sample example right?
Yes it is.
So there we go.
So here you have it.
Hours and rate equal pay.