I have posted this on a different site, but haven't received an answer there...so posting here to get an answer that will help me understand what is going on. My original post is below:
So, I did my own code for this before going back and viewing the video of the instructor walking through this exercise.
My code:
(01) hours = input('How many hours did you work?')
(02) rate = input('How much do you make per hour?')
(03) if float(hours) <= 40:
(04) strhrs = hours # strhrs = straight-time hours
(05) else:
(06) reghrs = 40
(07) othrs = float(hours)-40 # othrs = over-time hours
(08) strhrs = 0
(09) if strhrs != 0:
(10) pay = float(strhrs)*float(rate)
(11) else:
(12) regpay = float(reghrs)*float(rate)
(13) otpay = (float(rate)*1.5)*float(othrs)
(14) pay = regpay otpay
(15) print(pay)
Then, I went back and watch the video covering this exercise. Obviously our codes are going to be a little different. Here is what the instructor coded:
Instructor's code:
(01) sh = input("Enter Hours: ")
(02) sr = input("Enter Rate: ")
(03) fh = float(sh)
(04) fr = float(sr)
(05)
(06) if fh > 40 :
(07) reg = fr * fh
(08) otp = (fh-40.0)*(fr*0.5)
(09) xp = reg otp
(10) else:
(11) xp = hours * rate
(12)
(13) print("Pay:",xp)
My question is, with the "otpay" (line 13 of my code) calculating the rate1.5, I'm getting the same result as "otp" (line 8 in the instructor's code) that calculates fr0.5.
What is the difference in how Python is handling the two pieces of code (rate * 1.5 VS float rate * 0.5)?
If I put in "1.5" in the instructor's code, the result is complete different (it calculates to 2.5 times the normal rate of pay instead of 1.5 times the normal rate of pay). I'm not seeing what the difference in syntax is that would cause this to happen.
CodePudding user response:
First, a comment on your code: What's the difference between straight-time hours and regular hours? There is none. You could simplify your code by not making that distinction. Also, all those float casts are unnecessary AFAICT.
Like this (untested)
hours = input('How many hours did you work?')
rate = input('How much do you make per hour?')
if hours <= 40:
reghrs = hours # reghrs = regular hours
othrs = 0 # othrs = over-time hours
else:
reghrs = 40
othrs = hours-40
regpay = reghrs*rate
otpay = (rate*1.5)*othrs
pay = regpay otpay
print(pay)
To answer your question: You're just using a different way to come to the same result. You're computing payment for the first 40 hours (regpay)
payment for the overtime time (otpay)
. Your instructor is computing regular payment for the time worked (reg)
additional payment for overtime (otp)
.
Btw: Please tell your instructor to use better variable names.