Python for Informatics: Exercise 3.1


Hello, and welcome
to a worked exercise for Python for Informatics,
Exploring Information. I’m your host,
Charles Severance. And this material is
copyright and Creative Commons attribution. So again, you’re supposed to
try to work these exercises yourself before
you attempt them. These are good crutches to
make sure 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 in your text editor. So here’s our exercise. We’re going to do the classic
rate times pay with over time. And so the idea is that
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 Text Wrangler and start a command line. I will close that. Close our drawer. Make it smaller. And I’m going to 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? 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– rate and the hours. I’ll put that print
statement back in. And the problem is 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, then we can say pay equals rate times hours. That’s fine. So now let’s come up– I
need to add four spaces. Here we go. I could write another
statement, say it like this. Actually, this should be
less than or equal 40. If pay is greater
than 40, we can get a different calculation. And that one is pay. 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 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. So we got pay is
rate times hours. And pay is equal to rate times
1.5 times hours minus 40. So let’s go ahead and run this. I got Python hours. Let’s do 30 hours and $10. That’s looking good. Now 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. If hours is less than 40. I’m confused. What’s wrong with this? Well, whoops. What did I do here? If pay is greater than 40. That does not seem right. Did I save this file? I’m crazy. 50 hours. $10 an hour. Oh, OK. I must not have saved the file. So pay is not defined on Line 8. Oops, I was typing too fast. That needs to be
hours greater than 40. 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. 50 hours at $10 an hour. That would be like
600 and something. This does not look
like the right number. And so I want to just
take this print back out. Print rate and hour. Save that. Let’s run it again. 50 hours. $10 per hour. $1,100. That does not look right. So let’s take a look at this. Rate times 40. That’s pretty obvious. That would be $400. Rate times one and a half. That would be $15 times the
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. Because we’re
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 to 40, so then they’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. That looks pretty much better. So we’re done with that. We had that little mistake there
of the parenthesis not working. And the other thing is is
that the much better way to write this is just
putting an else here. So we just say else. And so now we have
an if, then, else. If it’s 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.

3 thoughts on “Python for Informatics: Exercise 3.1

  1. Chuck, thanks for posting "mistakes" as you go on these worked assignments. It really helps me as a beginner to see these and how you troubleshoot them.

  2. Eu fiz errado? O dele deu menos que o meu

    calc = int(hour) * float(price)
    if int(hour) >= 40:
    calc2 = float(price) * 1.5 * float(hour)
    print ("o valor é diferenciado: ", calc2)
    else:
    print ("o valor a ser pago é: ", calc)

Leave a Reply

Your email address will not be published. Required fields are marked *