3.1 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay the hourly rate for the hours up to 40 and 1.5 times the hourly rate for all hours worked above 40 hours. 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 - assume the user types numbers properly.

Python Program to Calculate Pay with Overtime

When working with payroll systems, employees are often paid extra money for overtime work. Overtime usually means that if a person works more than a standard number of hours (commonly 40 hours per week), they receive higher pay for the extra hours.

In this tutorial, we will learn how to write a Python program that calculates total pay including overtime. The program will ask the user to enter the number of hours worked and the hourly rate. If the employee works more than 40 hours, the extra hours will be paid at 1.5 times the regular rate.

This example introduces several important Python concepts such as user input, conditional statements, arithmetic operations, and data type conversion.


Python Program

Below is the Python code used to calculate pay including overtime:

hrs = input('Enter hours: ')
rts = input('Enter rates: ')
h = float(hrs)

if h <= 40:
pay = h * float(rts)
else:
hnew = h - 40
pay = (40 * float(rts)) + (float(hnew) * (float(rts) * 1.5))

print(pay)

Step-by-Step Explanation

Let us understand how this program works step by step.


1. Taking Input from the User

The first two lines ask the user to enter the number of hours worked and the hourly rate.

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

The input() function reads data from the user through the keyboard.

Example input:

Enter hours: 45
Enter rates: 10

However, the values entered using input() are stored as strings (text).

For example:

hrs = "45"
rts = "10"

Since mathematical operations require numbers, we must convert these values.


2. Converting Input to Float

The next line converts the number of hours into a numeric value.

h = float(hrs)

The float() function converts a string into a decimal number.

Example:

float("45") → 45.0

Now Python can perform calculations using this value.


3. Checking if Overtime Exists

The program uses an if condition to check whether the employee worked more than 40 hours.

if h <= 40:

If the employee worked 40 hours or less, there is no overtime.

In this case, the pay is calculated using the standard formula:

Pay = Hours × Rate

Example:

Hours = 35
Rate = 10

Pay = 35 × 10
Pay = 350

The program performs this calculation with:

pay = h * float(rts)

4. Calculating Overtime Pay

If the employee works more than 40 hours, the program executes the else block.

else:

First, the program calculates the extra hours worked.

hnew = h - 40

Example:

Total Hours = 45
Regular Hours = 40
Overtime Hours = 5

So:

hnew = 45 - 40 = 5

5. Calculating Total Pay

The total pay includes:

  1. Regular pay for the first 40 hours

  2. Overtime pay for additional hours at 1.5× rate

The formula becomes:

Total Pay = (40 × Rate) + (Overtime Hours × Rate × 1.5)

This is implemented in the program as:

pay = (40 * float(rts)) + (float(hnew) * (float(rts) * 1.5))

Example calculation:

Hours = 45
Rate = 10

Regular Pay = 40 × 10 = 400
Overtime Pay = 5 × 10 × 1.5 = 75

Total Pay = 400 + 75
Total Pay = 475

6. Displaying the Result

Finally, the program prints the total pay.

print(pay)

Output example:

Enter hours: 45
Enter rates: 10
475.0

Example Program Execution

Example 1 (No overtime):

Enter hours: 35
Enter rates: 10
350.0

Example 2 (With overtime):

Enter hours: 45
Enter rates: 10
475.0

Why Overtime Calculation is Important

Overtime pay is widely used in payroll systems. Many companies pay employees extra for working beyond normal hours.

Programs like this are used in:

  • Payroll management systems

  • Employee salary software

  • HR management tools

  • Work hour tracking applications

Understanding how to implement overtime calculations is an important programming exercise.


Conclusion

This Python program demonstrates how to calculate employee pay including overtime. It uses user input to collect data, converts strings into numeric values, and applies conditional logic to determine whether overtime pay should be applied.

The program also introduces beginners to the if-else statement, which is one of the most important decision-making structures in programming.

By practicing programs like this, beginners can improve their understanding of Python and build a strong foundation for more advanced programming topics.

Post a Comment

0 Comments