Computing a Paycheck Using Python
A User's Bi-weekly Paycheck
Annual Income | Tax Due (Single) | |
0.00 to 10000.00 | 5% of annual income | |
10000.01 to 50000.00 | $500 plus 10.5% of income over 10000 | |
Over 50000.00 | $4700 plus 20.25% of income over 50000 |
Annual Income | Tax Due (Married) | |
0.00 to 25000.00 | 6.25% of annual income | |
25000.01 to 90000.00 | $1562.50 plus 10.75% of income over 25000 | |
Over 90000.00 | $8550.00 plus 22.5% of income over 90000 |
Remember that your calculation from these tables will give you an annual tax amount and you need to adjust it to represent the bi-weekly tax deduction.
You will ask the user for their salary, and whether they are married or single.
You should use input prompts to tell the user what they should enter.
The salary should be input as gross yearly salary, with no dollar signs or commas. When you read in the salary, you should account for the fact that the salary may be input as dollars and cents, eg, 42000.50.
The marital status will be entered as a single letter, 'M' or 'S' which represents married vs. single status.
You will compute the gross pay for a single paycheck, which covers two weeks. You will also need to compute the deductions (health insurance and taxes) for the paycheck and the net pay. You will then print these amounts as shown in the following example. Note: You should compute everything with as much precision as possible to save the amounts of fractional cents, but in order to print in just dollars and cents, you will need to round to the nearest penny (2 decimal places). An example of using the round function to 2 decimal places is as follows (assuming the computed net pay is in the variable net_pay):
rounded_net_pay = round(net_pay,2)
Once again, do your computations without rounding, but round only when it is time to print out the amounts. You should print a message indicating the gross pay, the deductions, and the paycheck amount, as in the following example:
For inputs of 42000.55 and M, you would print out
For this pay period:
The gross pay is $1615.41
Total deductions are $168.85
The paycheck is $1446.56
Remember that white space is important!
IMPORTANT NOTE: You should start this project right away, even though we have not covered conditionals in chapter 4 yet. You should write the program assuming that everyone has a salary over $25,000 but less than $90,000, and is married. Get that working completely. If you do, you will pass test cases 1 and 3, and you can be confident that your expressions and logic is correct for a single case in the table. Then, once you have read chapter 4, you can change your program and add in the code you will need to cover the other cases.
Python Code Solution
gross_annual_salary = float(input("Enter your annual salary: "))
marital_status = input("Enter 'M' if you are married or 'S' if you are single: ")
# Identify the health insurance and tax
health_insurance = 0
tax = 0
if marital_status == 'S' or marital_status == 's':
health_insurance = 800
if gross_annual_salary > 50000:
tax = 4700 + (gross_annual_salary - 50000) * 0.2025
elif gross_annual_salary > 10000:
tax = 500 + (gross_annual_salary - 10000) * 0.105
else:
tax = gross_annual_salary * 0.05
elif marital_status == 'M' or marital_status == 'm':
health_insurance = 1000
if gross_annual_salary > 90000:
tax = 8550 + (gross_annual_salary - 90000) * 0.225
elif gross_annual_salary > 25000:
tax = 1562.50 + (gross_annual_salary - 25000) * 0.1075
else:
tax = gross_annual_salary * 0.0625
# Calculate net pay and weekly salary
# Note: There are 52 weeks in a year
gross_bi_weekly_salary = (gross_annual_salary / 52) * 2
health_insurance_bi_weekly = (health_insurance / 52) * 2
tax_bi_weekly = (tax / 52) * 2
net_bi_weekly_salary = gross_bi_weekly_salary - health_insurance_bi_weekly - tax_bi_weekly
# Print out results
print()
print("For this pay period: ")
print(" The gross pay is $" + str(round(gross_bi_weekly_salary, 2)))
print(" Total deductions are $" + str(round(health_insurance_bi_weekly + tax_bi_weekly, 2)))
print(" The paycheck is $" + str(round(net_bi_weekly_salary, 2)))