Tax Calculation using Loops and Decisions
Assignment:
Write a Python program to use loops and decisions.
Use Python for the following problem helping an IRS agent to calculate income tax based on user income and filing status. We will use loops to process more than one set of input values.
- Ask for the number of people that we would like to process. Make sure the user inputs a number between 1 and 20 for the number of people. You need to continue asking for a valid number of people until valid data is entered.
- For each person, in the following order, ask for the filing status (single or married) and income for the person. Assume the user will only enter data in lower case for filing status.
- If the filing status is entered as anything but single or married, continue asking until a valid filing status is entered.
- Use the following table to calculate tax:
- Calculate tax due.
- Display filing status, income, and the amount of tax due.
- Calculate and display the grand total of all income values entered by the user.
- Calculate and display the grand total of all tax values entered by the user
Display the following information in a format very close to the following sample run. For example, you need to display the labels as stated, below. Make sure to test for all possible cases to assure your program is functioning properly.
Sample runs:
Number of people: -1
ERROR – try again,
Number of people: 30
ERROR – try again,
Number of people: 1
Filing status: single
Income: 60000
Filing status: single
Income: $60000.00
Tax: $3700.00 <
Filing status: joint
Invalid filing status – try again
Filing status: married
Income: 150000
Filing status: married
Income: $150000.00
Tax: $11000.00 <
Filing status: single
Income: 20000
Filing status: single
Income: $20000.00
Tax: $2000.00 <
Grand Total of income = $230000.00
Grand Total of tax = $16700.00
Solution:
# Read number of people between 1 and 20
num_people = int(input("Number of people: "))
while num_people < 1 or num_people > 20:
print("ERROR - try again,")
num_people = int(input("Number of people: "))
# Process each person
grand_total = 0
grand_tax = 0
for i in range(num_people):
# Get status if single or married
status = input("Filing status: ")
while status != "single" and status != "married":
print("Invalid filing status - try again")
status = input("Filing status: ")
# Get the income
income = float(input("Income: "))
while income < 0:
print("ERROR - try again,")
income = float(input("Income:"))
# Calculate the taxes
if status == "single":
if income > 100000:
tax_due = 6000 + ((income - 100000) * 0.15)
elif income >= 50000:
tax_due = 2500 + ((income - 50000) * 0.12)
else:
tax_due = income * 0.10
elif status == "married":
if income > 100000:
tax_due = 6000 + ((income - 100000) * 0.10)
elif income >= 50000:
tax_due = 2500 + ((income - 50000) * 0.08)
else:
tax_due = income * 0.05
grand_total += income
grand_tax += tax_due
print("Filing status: " + status)
print("Income: $" + "{:.2f}".format(income))
print("Tax: $" + "{:.2f}".format(tax_due))
print()
print("Grand Total of income = $" + "{:.2f}".format(grand_total))
print("Grand Total of tax = $" + "{:.2f}".format(grand_tax))