264. Python Program to Calculate Payroll with Overtime and Tax
HardPhase 3 · Challenge
This one takes a few steps, and each step is something you know: functions with return, a while loop, split() and :.2f. Work out one worker's pay on paper first.
Overview
Payroll is the job of working out what every worker is paid. Real payroll programs follow exact rules for extra hours and for tax, and they keep each rule in its own function.
Problem Statement
Marta runs the payroll for a small workshop. Each week, Marta types one line per worker: the name, the hours worked and the rate (the coins paid for one hour).
The pay rules:
- The first 40 hours are paid at the rate.
- Every hour above 40 is overtime. It is paid at 1.5 times the rate.
The tax rules, on the week's pay. A slab is a band of pay with its own tax:
- The first 500 coins: no tax.
- The part from 500 to 1000 coins: 10% tax.
- The part above 1000 coins: 20% tax.
The net pay is the pay minus the tax.
Write two functions and use them:
weekly_pay(hours, rate)returns the pay.tax_on(pay)returns the tax.
First print the header Name | Pay | Tax | Net. Then, for each worker, print <name> | <pay> | <tax> | <net>. At the end, print Total paid: <total>, the sum of all the net pays. Print every amount with 2 decimal places.
Examples
Example 1. Input:
Ana 40 10
Ben 45 20
Chen 50 30
end
gives:
Name | Pay | Tax | Net
Ana | 400.00 | 0.00 | 400.00
Ben | 950.00 | 45.00 | 905.00
Chen | 1650.00 | 180.00 | 1470.00
Total paid: 2775.00
Why: Ben gets 40 × 20 = 800, plus 5 overtime hours × 30 = 150, so 950. Ben's tax is 10% of 450 = 45. Chen gets 1200 + 10 × 45 = 1650. Chen's tax is 10% of 500 = 50, plus 20% of 650 = 130, so 180.
Example 2. Input:
Dev 41 15
Eva 0 25
end
gives:
Name | Pay | Tax | Net
Dev | 622.50 | 12.25 | 610.25
Eva | 0.00 | 0.00 | 0.00
Total paid: 610.25
Why: Dev gets 40 × 15 = 600, plus 1 hour × 22.5. The tax is 10% of 122.50 = 12.25. Eva worked 0 hours, so Eva gets nothing.
Input
One line per worker: a name (one word), the hours (a whole number, 0 or more) and the rate (a whole number, 1 or more), with one space between them. The last line is end. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
The header, one line per worker in the order they were typed, then the Total paid: line. With no workers, print only the header and Total paid: 0.00. The tests try 7 different inputs, also pay of exactly 500 and 1000, and a worker with 44 hours, where only the 4 extra hours get the higher rate.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.