Worked Exercise 3.1

preview_player
Показать описание

Please visit the web site to access a free textbook, free supporting materials, as well as interactive exercises.
Рекомендации по теме
Комментарии
Автор

While you are known for clarity in your lectures, leaving mistakes in your videos drive home that real world experience of trying new things. This series is gold as you continuously make lights turn on. Thank you for making this series and your book readily available.

daviddaytona
Автор

Just thought this might be easier for some people to understand:
hrs = float(input('Enter hours: '))
rate = float(input('Enter rate per hour: '))
pay = hrs * rate

if hrs > 40:
above_40 = hrs - 40
pay = 40 * rate + above_40 * rate * 1.5
print("Overtime")
else:
print("regular")

print(pay)

mahadialam
Автор

Sir many thanks for keeping those mistakes in your videos. They not only made us laugh but also feel better about ourselves. As a novice programmer, you can not imagine how helpful this ego boost is for us.

tohameem
Автор

Sir I'm taking your course at courser and its absolutely brilliant. I started liking Python because of you, how you teach Python by making it really funny like how you told the story about you being a slithering not a griffendor in your class of "Python as a language". Sir you're amazing.

anshgoyal
Автор

I love the way you teach, even if you mess up with your program, you carry out it with fun 😂✌️

jerrinagabriel
Автор

I've just started your course and it's very helpful, thank you! I just wanted to let you know that on the exercise explanation in autograder for ex_03_01 it recommends "Pay the hourly rate for the hours up to 40 and 1.5 times the hourly rate for all hours worked above 40 hours." rather than the 0.5 that you advise in the video. This could be why other people are also getting errors that I can see in the comments. Thanks Ash

ashleighcooper
Автор

my suggestion to make this easier:
first do (hours - 40) to get the extra hours
then for the just 40 hours multiply by the regular rate
and seperately for extra hours multiply by (regular rate * 1.5 )

add the pay for just 40 hours and the extra hours together to get total pay

aldrinseanpereira
Автор

I think chuck got it wrong. the regular pay should list as 400 and overtime pay 150 (10 hrs x 15 usd). this is how I wrote the code:

hrs = input("Enter Hours:")
rate = input("Enter Rate:")
hr = float(hrs)
rt = float(rate)
base0 = 40 * rt
base = hr * rt
op = (hr - 40) * rt * 1.5
tot = op + base0
if hr > 40:
print('Overtime Pay:', op)
print('Regular Pay:', base0)
print('Total:', tot)
else:
print('Pay:', base)

thagoff
Автор

Just for additional fun, if the code has to be shortened and done with less conditioning, we can remove the 'else' condition:

hrs = input ("Enter the number of hours: ")
h = float (hrs)
rt = input ("Enter rate of pay: ")
rate = float (rt)
pay = h * rate

if h > 40:
h2 = h - 40
pay = pay + (h2 * rate * 0.5)

print (pay)

vumevmeghan
Автор

There are no computational problems here and two formulas below for the above calculation.

1. [(Regular hrs + overtime hrs) * Rate] + [overtime hrs * rate * 0.5]
2. [Regular hrs * Rate] + [overtime hrs * rate * 1.5]

Both No.1 and 2 calculate the pay correctly and basically the same meaning.

The professor uses formula No 1. and have to multiply 0.5

If you want to multiply by 1.5 as you guys comment, you have to use formula NO.2

In conclusion, there is NO problem with the professor's calculation result.

Mattmath
Автор

# if you use regular_hour=40, Regular_hour=10,
#Then, if the overtime is 45 hrs, then Regular calculation without overtime will be regular_hour = 45 * 10,
#Overtime=450 +((45-40)*(10*0.5))
#half of the overtime i.e. 450-400=50, 50 is the overtime money,
#So, Chuck said half of the overtime, i.e., 50*0.5, half of 50 is 25.

Otherwise, you can write the code this way.
hrs = input("Enter Hours:")
h = float(hrs)
xx = input("Enter the Rate:")
x = float(xx)
if h <= 40:
print( h * x)
elif h > 40:
print(40* x + (h-40)*1.5*x)

palashsharma
Автор

this is how i did it:
hrs = float(input('How many hours? '))
rate = float(input('What is the rate? '))
extra = 0.00
if hrs>40:
extra = (hrs-40) * 1.5 * rate
hrs = 40
pay = hrs * rate + extra
print(pay)

kristiann
Автор

FYI: The result of $475 is correct, and the logic is sound. It didn't initially make sense to me, possibly because as a wage labourer, I'd want to know my total OT rate per hour (base pay + OT pay), not just the amount I make in OT only (without the base pay added in). The first number is higher, which would make me feel less poor if I saw it on my pay stub. :D

It also seems common to separate one's regular hours/pay from money made working overtime in IRL, eg. on a pay stub (presumably, a more complex version of the code that's demonstrated here would be used in accounting/payroll software). OT tends to show up on a pay stub as the total hourly OT rate (ie. the sum of the base pay plus the OT dollars; the hourly OT rate is $15/hour using the numbers from the example here). You'll also see the total value of the OT hours you worked (which is a product of your OT hours and your OT rate). This is usually listed separately from your regular pay.

I worked out this problem by simplifying the relevant expressions (on paper!). I'll start with the expression fr*fh. For the sake of this example, I'll assume fh = 45 (so we're working with overtime hours and pay). Chuck accounts for the base pay for all 45 hours in this expression:

reg = fr*fh
= 10 * 45
=450

This initially threw me off because I see this problem differently, ie. I separate the regular pay (when hours are <= 40) and the OT pay. Seen this way, your regular pay can only add up to $400 max ($10 * 40h = $400). Once you work more than 40 hours, you're working at a higher rate ($15/hour). But the way Chuck worked this problem, he's accounting for the base pay for all of the hours worked (regular and overtime) with the first expression (above). (Chuck and I both get the same - correct - answer with our respective versions of this code.)

Here's the second expression in question, simplified (as Chuck performed the calculations, ie. multiplying fr by 0.5).
otp = (fh - 40) * (fr * 0.5)
= (45-40) * (10 * 0.5)
= 5 * 5
= 25

At first glance, this makes it look like you're only making $5/hour by working overtime. But this isn't your *total* OT wage (since the base wage is excluded here; it's excluded here because it was included in the first expression). In other words, because he accounted for the base pay for all 45 hours in the first expression, all he has to account for here is the additional money made working over time, ie. you make an extra $5/hour by working 5 hours of overtime, which gives you $25 of extra money in total. In other words, this expression is saying that when you work OT, you make $5/h *in addition* to your base rate of $10/h.

The third expression, simplified:
xp = reg + otp
= 450 + 25
= 475

The total pay (base pay plus additional money made working 5 hours of OT) is $475.

I did this differently, which also works:

First expression, simplified:
reg = fr * 40
= 10 * 40
=400

I used the constant 40 to refer to the regular hours, and therefore to calculate the regular (pre-overtime) pay. My reasoning: if you're working OT, you always have to work that first 40 hours first. That never changes (where OT is concerned), so it makes sense to make that value a constant. At $10/hour, your first 40 hours of work brings in $400.

Second expression, simplified:
otp = (fh - 40.0) * (fr * 1.5)
= (45-40) * (10 * 1.5)
= (5) * (15)
= 75

Chuck used 0.5 because he accounted for the base pay for the entire 45 hours in the first expression (if you write the code out Chuck's way but use 1.5 instead of 0.5, your total pay will be too high). Since I only accounted for the first 40 hours in my version of the first expression (and not the total 45 hours, like Chuck did), I have to use 1.5 to account for the total pay for each overtime hour (ie. base rate plus OT rate = 1.5). This gives me $15/hour as the total OT wage. In other words, instead of $5/hour of additional pay for each OT hour (as in Chuck's example), I'm using the total pay for each OT hour (base rate plus additional OT pay, which equals $15/hour). Then I'm multiplying $15/hour by the number of OT hours worked (5 hours), which gives a total of $75 of OT pay in total.

Third expression, simplified:
xp = reg + otp
= 400 + 75
= 475

Chuck and I both get the same answer for the total pay, ie $475. We got there in different ways, but they're both logically sound. The adventure one would choose would likely depend on how they think about the problem, and what they want to do with all of the data the program receives, processes, and generates.

Hopefully that helps people who were wondering why and how one would use either 0.5, or 1.5 in the second expression. Use 0.5 if you're accounting for the total hours worked all at once (ie. all 45 hours, as in Chuck's example). Use 1.5 if you want to separate the total value of the regular hours worked from the total value of the overtime hours worked (as in my example). You just have to change how you calculate the regular pay (in the first expression) accordingly, or else the result for the total pay will be incorrect.

RheaRevolver
Автор

Thank goodness you made this video! I didn't know that there was supposed to be an extra input command for the rate. I was just putting the rate in the script instead of asking the user to put it in. Thanks!

CalebHawn
Автор

I still don't umderstand the *0, 5. Could someone explain me?

flaviobrienza
Автор

Thanks for keeping the mistakes in the video, this really shows us that we are all capable of these simple errors.

stephanieluna
Автор

This also serves as a lesson that it's always worth the little extra time upfront to think of good variable names. Future you will thank you.

DaveMcAnulty
Автор

it actually made me laugh when i watched him go try and fix the error and put it back to the exact same thing and then run the exact same program. i couldnt wait for him to see it again

skelly
Автор

Another alternative:


hrs = input('Enter Hours:')
h = float(hrs)
rt = input ('Enter rate:')
r = float(rt)
if h > 40:
ot = r * 2.5
calc = h * r
total = (ot + calc)
else:
total = (h * r)
print (total)

maitilupas
Автор

I gotta say this helped a lot! I was stuck in that exercise and although I was in the right path I was lacking more lines of code. I’m enjoying this but sometimes makes me rage quit for some days 🤣🤣🤣🤣

joebyron