Python Forum

Full Version: Hello all! need help. calculating OT hours.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So, I'm "self teaching" python, and I'm trying to challenge myself. I'll take the answer but a "hint" would be more helpful in the long run. So, I'm making a simple pay calculator program. I have some simple code, but I don't know how to add the "OT = over time" hours in. I'd like to calculate anything over 40 hours gets time and a half. so the following is my code.

rate = 10
ot_rate = 15
time = input("please enter your time: ")
time_int= int(time)
pay = int(rate) * int(time)

if time_int > 40:
    
    print(f" Your pay is ${}")
else:
    print(f" Your pay is ${pay}")
Okay, well, as a hint...

over time = total hours worked minus 40

So, you only need to calculate the over time pay if over time is grater than zero, else just calculate the standard pay.

You can also do on-the-fly type conversions:

hours = int(input("please enter your time: "))
I'd use float instead of int. I want to get paid for my entire 2.5 hours of overtime.

I would not use if or else to solve this problem. I would use min and max.

The most common error I see with this problem forgetting that overtime rates are only applied to overtime hours and regular rates to regular hours. No double dipping!
(Jul-30-2022, 05:00 PM)deanhystad Wrote: [ -> ]I'd use float instead of int. I want to get paid for my entire 2.5 hours of overtime.

Good point
(Jul-30-2022, 04:29 PM)rob101 Wrote: [ -> ]Okay, well, as a hint...

over time =( total hours worked minus 40) * 15

So, you only need to calculate the over time pay if over time is grater than zero, else just calculate the standard pay.

You can also do on-the-fly type conversions:

hours = int(input("please enter your time: "))

Thank you! it actually hit me when I was walking around the grociery store.. I'm like, what if I make "OT = time - 40" then add "OT" only if its greater than 40!. then I seen your email and that confirmed it!

I have it all set to int for now to test the code cleanly, then I'll tweak it. I'm just learning this is day 4 for me. I've never coded before so, I have a long way to go!
(Jul-30-2022, 09:21 PM)no1up Wrote: [ -> ]Thank you! it actually hit me when I was walking around the grociery store.

No worries.

I find that switching off from a problem, by doing something completely different, also works for me; usually it's about 3am and I'm awaken by a possible solution, which I have to try.

Keep learning and coding and if you need help, just ask.

Python is a fantastically versatile language, so much so, that there's usually two, three, four and more ways to solve a problem, but remember to K.I.S (Keep It Simple) and apply the principle of Occam's razor.