How do I correct this Python pay calculator without changing the overtime rate in the formula?

Question:

I am attempting to write a program that calculates pay, with the option of overtime for more than 40 hours at $10/hour. Currently it looks like this:

hrs = input("Enter Hours: ")
h = float(hrs)
rate = input("Enter Rate: ")
r = float(rate)

if h > 40:
    reg = h * r
    otp = (h - 40) * (r * 1.5)
    final_otp = reg + otp
    print("Pay:",final_otp)

else:
    reg_pay = h * r
    print("Pay:",reg_pay)

The problem is that when using this specific method, it causes regular pay at overtime to be 450 when it is really 400, adding 75 for overtime giving a total of 525. One solution would be to change the overtime rate formula to 0.5, but this doesn’t really make sense to me and I would like to see it done using a rate of 1.5. How do I correct it while still multiplying by the 1.5 rate?

I put in 0.5 for the OT rate multiplier but I’d like to see how it can be done without doing so. I don’t do very well at math so if I were to make a more complex version of this, I wouldn’t know how to correct the OT rate in the formula to compensate.

Asked By: Connor

||

Answers:

It looks like you are focusing too hard on the derivation of the regular pay when the entered hours exceeds the normal threshold for calculating overtime. After reviewing and testing your code, following is a refactored version of your program with some small tweaks to make it a bit more robust.

ot_level = 40

print("Enter hours and rate to calculate payroll or enter "q" to end payroll")

while True:
    hrs = input("Enter Hours: ")
    if hrs == 'q':
        break
    h = float(hrs)
    rate = input("Enter Rate: ")
    r = float(rate)

    if h > ot_level:
        reg = ot_level * r              # Just multiply the normal pay hours value by the rate for regular pay
        print("Regular pay:", reg)
        otp = (h - ot_level) * (r * 1.5)
        print("Overtime:", otp)
        final_otp = reg + otp
        print("Pay with overtime:",final_otp)

    else:
        reg_pay = h * r
        print("Pay:",reg_pay)

Following are some items to note.

  • Though forty hours is the normal threshold in payroll calculations utilizing a constant value initialized to forty hours provides some flexibility to the program if it were to be used elsewhere where the overtime threshold might be a different value.
  • The entry of times and rates is placed within a "while" loop to allow for multiple payroll calculations so that the program doesn’t need to be constantly restarted for each payroll calculation – again just adding some some simple usability and also illustrating a nice use for a "while" loop.
  • Within the test where the entered hours exceeds the overtime threshold, the calculation of regular pay would simply be the overtime threshold value multiplied by the entered rate (in this case forty hours multiplied by the entered rate).
  • Some additional print statements were added just for visual reinforcement of the process – these easily can be omitted.

With those bits of refactoring, following was the test output at the terminal.

@Vera:~/Python_Programs/Payroll$ python3 Payroll.py 
Enter hours and rate to calculate payroll or enter "q" to end payroll
Enter Hours: 40
Enter Rate: 10
Pay: 400.0
Enter Hours: 45
Enter Rate: 10
Regular pay: 400.0
Overtime: 75.0
Pay with overtime: 475.0
Enter Hours: 42
Enter Rate: 12
Regular pay: 480.0
Overtime: 36.0
Pay with overtime: 516.0
Enter Hours: q

Go ahead and evaluate this refactored code and see if it meets the spirit of your project.

Answered By: NoDakker

This sounds more like a math question than a python one. JonSG correctly points out that to fix the code, you can change reg = h * r to be reg = 40 * r. Perhaps a discussion about why this fixes the code is worthwhile:

Overtime pay is calculated as the regular rate for the first 40 hours, and an increased rate (r * 1.5) after 40 hours. So to calculate the regular pay in an overtime situation, the maximum number of hours worth of regular pay is 40. The problem with the code is that it double counted the 5 hours of overtime. Paying 45 hours worth of regular pay and 5 hours of overtime.


Another way to think about this is that all hours of work are paid at the base rate. And any hours over 40 are also paid an overtime bonus (0.5 * r). This is why changing to multiply by 0.5 produces the correct output in the original code.

Going with this idea, you could calculate the total pay as follows:

hrs = input("Enter Hours: ")
h = float(hrs)
rate = input("Enter Rate: ")
r = float(rate)

base = h * r
hours_of_overtime = max(h-40, 0)  # if h < 40, 0 hours of overtime
overtime_bonus = hours_of_overtime * 0.5 * r  
total_pay = base + overtime_bonus
print("Pay:", total_pay)
Answered By: cstoltze
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.