Income Tax Calculator Challenge
Income Tax Calculator Challenge
Python Programming Challenge
The objective of this Python programming challenge is to create a program to calculate the annual income tax of the user.
Write a Python program to calculate and display the income tax of the user based on his annual income and his individual filing status..
The tax rates and the allowances are stored in two dictionaries:
tax_rate = {5:18000 20:25000, 30:40000, 35:50000, 42: 65000}
deduction = {“mortgage”:5, “kids”: 15, “married”: 9}
The program will ask the user to enter the following:
1) What is your annual income?
The program should calculate the income tax based on the “tax_rate” dictionary where the key is the percentage and the value is the maximum income that can be applied to each tax rate.
For example, if the income is less than 18000 then a 5% rate applies. If the income input by the user is greater than 18000 and less than 25000, then apply a 20%.
2) Do you have a mortgage? yes/no
3) Do you have children under 10 years old?
4) What’s your legal status? Select one: “single”, “married”, “divorced”, “widow”
Based on the user responses, the program will subtract from the income tax calculated in step 1 the corresponding percentage according to deductions dictionary.
For example, if the user has a mortgage, he can reduce the total amount by a 5%. If the user has kids younger than 10 years old he gets a 15% reduction and so on.
Expected Output
What is your annual income? 36000
Do you have a mortgage? (yes/no) yes
Do you have children under 10 years old? (yes/no) no
What’s your legal status? Select one: “single”, “married”, “divorced”, “widow”
Your calculated income tax after deductions is: 2090.0
Source Code
# Tax rates and deductions
tax_rate = {5: 18000, 20: 25000, 30: 40000, 35: 50000, 42: 65000}
deduction = {'mortgage': 5, 'kids': 15, 'married': 9}
# Accept user input
income = float(input("What is your annual income? "))
has_mortgage = input("Do you have a mortgage? (yes/no) ")
has_kids = input("Do you have children under 10 years old? (yes/no) ")
legal_status = input("What’s your legal status? Select one: “single”, “married”, “divorced”, “widow” ")
# Calculate income tax based on the tax rate
for rate, threshold in sorted(tax_rate.items(), reverse=True):
if income > threshold:
tax = (income - threshold) * (rate / 100)
break
else:
tax = 0
# Apply deductions based on the user's family situation
if has_mortgage.lower() == 'yes':
tax -= (tax * (deduction['mortgage'] / 100))
if has_kids.lower() == 'yes':
tax -= (tax * (deduction['kids'] / 100))
if legal_status.lower() == 'married':
tax -= (tax * (deduction['married'] / 100))
# Display the calculated income tax
print(f"Your calculated income tax after deductions is: {tax}")
Click here for more real Python projects.
