Module 5 Assignment 3.1 Answer
3.1 Compute Gross Pay
Question
Write a Python program that:
- Prompts the user for hours worked and hourly rate.
- Pays the regular rate for hours up to 40.
- Pays 1.5 times the hourly rate for hours above 40.
- Uses 45 hours and a rate of 10.50 per hour for testing (Expected Output:
498.75
).
Correct Python Code
# Prompt user for input
hrs = input("Enter Hours: ")
rate = input("Enter Rate per Hour: ")
# Convert input to float
h = float(hrs)
r = float(rate)
# Calculate pay
if h > 40:
overtime_hours = h - 40
overtime_pay = overtime_hours * (r * 1.5)
regular_pay = 40 * r
total_pay = regular_pay + overtime_pay
else:
total_pay = h * r
# Display output
print(total_pay)
Expected Output
Enter Hours: 45
Enter Rate per Hour: 10.50
498.75
Explanation
- Prompt the user for hours worked (
hrs
) and hourly rate (rate
). - Convert input to float to allow calculations.
- Check if hours exceed 40:
- Compute regular pay (
40 * rate
). - Compute overtime pay (
extra hours * 1.5 * rate
). - Sum both to get total pay.
- Compute regular pay (
- Print the final pay amount.
Why This Code is Correct
✅ Handles overtime calculation correctly.
✅ Uses float() conversion for accurate calculations.
✅ Produces expected output (498.75
) when tested with 45
hours and 10.50
rate.