Python Forum
Hello all! need help. calculating OT hours.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hello all! need help. calculating OT hours.
#1
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}")
Larz60+ write Jul-30-2022, 10:06 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
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: "))
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
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!
Reply
#4
(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
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
(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!
Reply
#6
(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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  MatplotibTicks every 24 hours TamP 4 1,161 Oct-04-2022, 04:08 PM
Last Post: deanhystad
  Py script that triggers ever 3 hours, but only between 9:15 am to 3:30 pm, Mon to Fri Pymod 2 1,840 Jun-30-2021, 05:14 PM
Last Post: Larz60+
  How to get utcnow + 2 hours? korenron 2 2,551 Mar-01-2021, 03:22 PM
Last Post: korenron
  Need to add hours min and seconds tester_V 5 3,115 Jun-02-2020, 05:29 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020