Python Program to Compute Pay with Overtime

4.6 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of pay in a function called computepay() and use the function to do the computation. The function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly. Do not name your variable sum or use the sum() function.

Python is a powerful, easy-to-learn programming language widely used for problem solving, automation, and real-world applications. A common programming task is calculating an employee’s salary based on the number of hours worked and the hourly pay rate.

In many organizations, employees receive normal wages for the first 40 working hours in a week. Any additional hours are considered overtime, and overtime work is usually paid at a higher rate, typically 1.5 times the normal hourly wage.

This Python program calculates an employee’s total pay, including overtime earnings when applicable. The program demonstrates important programming concepts such as user input, data type conversion, functions, and conditional statements.

Infographic explaining Python program to compute employee pay with overtime calculation including function logic and example output
Python program example showing overtime pay calculation using functions and conditional statements.

1. Taking User Input

The program begins by asking the user to enter:

  • Number of hours worked
  • Hourly pay rate

The input() function is used to accept values from the user. Since input() reads data as a string, the values must be converted into numeric form before performing calculations.

hrs = input('Enter hours: ')
rts = input('Enter rate: ')

2. Converting Data Types

Mathematical operations cannot be performed directly on strings. Therefore, the program converts the entered values into floating-point numbers using the float() function.

h = float(hrs)
r = float(rts)

3. Defining the Function

A function named computepay() is created to calculate total wages. Functions help organize programs into reusable and manageable blocks of code.

def computepay(h, r):

The function accepts two parameters:

  • h → hours worked
  • r → hourly rate

4. Calculating Regular Pay

If the employee works 40 hours or fewer, the pay is calculated using the standard formula:

Pay = Hours × Rate

if h <= 40.0:
pay = h * r

5. Calculating Overtime Pay

If the employee works more than 40 hours, the extra hours are treated as overtime.

else:
hnew = h - 40.0
pay = (40.0 * r) + (hnew * (r * 1.5))

The calculation is divided into two parts:

  1. Regular pay for the first 40 hours
  2. Overtime pay at 1.5 × hourly rate

6. Returning the Result

The function sends the calculated pay back to the main program using the return statement.

return pay

7. Calling the Function and Displaying Output

The function is called, and the result is printed using the print() function.

pay = computepay(h, r)
print("Pay", pay)

8. Complete Python Program

hrs = input('Enter hours: ')
rts = input('Enter rate: ')

h = float(hrs)
r = float(rts)

def computepay(h, r):
if h <= 40.0:
pay = h * r
else:
hnew = h - 40.0
pay = (40.0 * r) + (hnew * (r * 1.5))
return pay

pay = computepay(h, r)
print("Pay", pay)

9. Example Execution

Input:

Enter hours: 45
Enter rate: 10

Calculation:

  • Regular Pay = 40 × 10 = 400
  • Overtime Hours = 5
  • Overtime Pay = 5 × (10 × 1.5) = 75

Total Pay = 475

Output:

Pay 475.0

10. Concepts Demonstrated

This program helps beginners understand:

  • User input handling
  • Data type conversion
  • Functions in Python
  • Conditional statements (if–else)
  • Real-world problem solving

Conclusion

This Python program efficiently calculates employee wages by considering both regular working hours and overtime pay. It is a practical example that demonstrates how programming logic and functions can be used to solve real-life calculation problems. Such programs help beginners develop a strong foundation in Python programming and computational thinking.

Post a Comment

1 Comments