BYU Student Author: @Jae
Reviewers: @MitchFrei, @Jonathan_Weston, @klayton
Estimated Time to Solve: 40 Minutes
We provide the solution to this challenge using:
- Python
This uses Google Collab as the Integrated Development Environment (IDE). The challenge requires no additional libraries/packages than those native to the python library.
Need a program? Click here.
Overview
It was a Monday morning like any other, and you sat down at your desk, ready to tackle the day’s accounting tasks. Perhaps you would order lunch that day from your favorite restaurant, Little Chicken. But as you logged onto your computer, you noticed a news alert flashing on your screen. “BREAKING NEWS: Tax Brackets Changed Overnight!”
Your heart began to race as you read on. Congress had passed a new tax bill late last night, and the changes were set to take effect immediately. As a junior accountant for a tax preparation company, you wondered how your team would update their tools and stay ahead of the competition.
That’s when your manager appeared at your desk, looking anxious and stressed. “We need a federal income tax calculator in Python, and we need it now!” she said. “With the new tax laws in place, our customers are going to need accurate and reliable calculations more than ever.”
Your mind started racing as you began to think about the coding challenge ahead. You knew that calculating federal income tax was quite simple for your company’s customer-base, but building the calculator in Python was no easy feat. As you rolled up your sleeves and started typing away at your keyboard, you couldn’t help but wonder what other surprises the day might have in store for you. Maybe you would get that lunch after all.
Instructions
Your manager has asked you to build a Federal Income Tax Calculator in Python. In order to build the calculator, you will require the following sections:
- Determine the appropriate standard deduction and income tax bracket based on filing status using the section of code below
Starting Code
def Fed_Income_Tax_Calc(): # Determine the appropriate standard deduction and income tax bracket based on filing status filing_status = input('What is your tax filing status? (single | married | head of household) ').lower() if filing_status == 'single': standard_deduction = 12950 #($min value of tax bracket, tax rate for bracket) tax_brackets = [(0, 0.1), (10275, 0.12), (41775, 0.22), (89075, 0.24), (170050, 0.32), (215950, 0.35), (539900, 0.37)] elif filing_status == 'head of household': standard_deduction = 19400 tax_brackets = [(0, 0.1), (14650, 0.12), (55900, 0.22), (89050, 0.24), (170050, 0.32), (215950, 0.35), (539900, 0.37)] elif filing_status == 'married': filing_status = input('Are you filing with your spouse? (seperately | jointly) ').lower() if filing_status == 'jointly': standard_deduction = 25900 tax_brackets = [(0, 0.1), (20550, 0.12), (83550, 0.22), (178150, 0.24), (340100, 0.32), (431900, 0.35), (647850, 0.37)] elif filing_status == 'separately': standard_deduction = 12950 tax_brackets = [(0, 0.1), (10275, 0.12), (41775, 0.22), (89075, 0.24), (170050, 0.32), (215950, 0.35), (323925, 0.37)] else: print('Invalid filing status, please try again.\n') Fed_Income_Tax_Calc() else: print('Invalid filing status, please try again.\n') Fed_Income_Tax_Calc()
- Determine income and deduction type (standard or itemized) and calculate the taxable income by subtracting the deduction
- Calculate the income tax owed using the tax brackets and corresponding tax rates
- Output the tax owed
In addition, note the following:
- The calculator is best built as a function in python
- Taxable income or taxes owed should not result in a negative value
- If the user makes a mistake, the function should continue or restart without the need to call it again
- The function should work regardless of the different ways a user may type an input such as SINGLE/Single/single/Etc
- The tax brackets are given in (minimum value of individual bracket, tax bracket rate) format as a list of tuples
- Federal Income Tax brackets can also be viewed here
- This challenge uses the 2022 brackets
- Assume that the user knows their total itemized deduction if they select this option
- Details like filing status, income, and what type of deduction the customer wants can either be collected as arguments/parameters in the function or as inputs by the user within the function code
- Output can be a returned value or a printed statement from the function
- Assume the user of the function cannot see your code and will only be inputting what is required to call the function and any other required inputs while the function is running (if applicable)
The following flowchart gives the overall flow of what your calculator is expected to do. The Starting Code found above covers the blue section of the flow chart:
Suggestions and Hints
- Check figure for single filer with standard deduction and income of $75,000: $9268.00
- This is a good resource to learn how to create a function in python
- Be aware of variable types. The default type for an input() function is string
- if/else conditional statements may be a good way to determine which brackets/deductions to use for each filing status
- for loops can also help calculate the tax owed if taxable income carries over multiple brackets
- a list of tuples can generally be referenced in the same way as a list of lists
Solution
Solution Code
def Fed_Income_Tax_Calc():
# Determine the appropriate standard deduction and income tax bracket based on filing status
filing_status = input('What is your tax filing status? (single | married | head of household) ').lower()
if filing_status == 'single':
standard_deduction = 12950
#($min value of tax bracket, tax rate for bracket)
tax_brackets = [(0, 0.1), (10275, 0.12), (41775, 0.22), (89075, 0.24), (170050, 0.32), (215950, 0.35), (539900, 0.37)]
elif filing_status == 'head of household':
standard_deduction = 19400
tax_brackets = [(0, 0.1), (14650, 0.12), (55900, 0.22), (89050, 0.24), (170050, 0.32), (215950, 0.35), (539900, 0.37)]
elif filing_status == 'married':
filing_status = input('Are you filing with your spouse? (seperately | jointly) ').lower()
if filing_status == 'jointly':
standard_deduction = 25900
tax_brackets = [(0, 0.1), (20550, 0.12), (83550, 0.22), (178150, 0.24), (340100, 0.32), (431900, 0.35), (647850, 0.37)]
elif filing_status == 'separately':
standard_deduction = 12950
tax_brackets = [(0, 0.1), (10275, 0.12), (41775, 0.22), (89075, 0.24), (170050, 0.32), (215950, 0.35), (323925, 0.37)]
else:
print('Invalid filing status, please try again.\n')
Fed_Income_Tax_Calc()
else:
print('Invalid filing status, please try again.\n')
Fed_Income_Tax_Calc()
income = int(input('What was your income for the tax year? '))
# Determine deduction type and calculate the taxable income by subtracting the deduction
deduction = input(f'Would you like to itemize or take the standard deduction of ${standard_deduction}? (itemize | standard)')
if deduction == 'itemize':
deduction = float(input('How much would you like to itemize?'))
elif deduction == 'standard':
deduction = standard_deduction
else:
print('Invalid deduction type, please try again.\n')
Fed_Income_Tax_Calc()
taxable_income = max(0, income - deduction)
# Calculate the income tax owed using the tax brackets and corresponding tax rates
tax_owed = 0
for i in range(len(tax_brackets)):
if taxable_income <= tax_brackets[i][0]:
break
elif i == len(tax_brackets) - 1:
tax_owed += (taxable_income - tax_brackets[i][0]) * tax_brackets[i][1]
elif taxable_income <= tax_brackets[i+1][0]:
tax_owed += (taxable_income - tax_brackets[i][0]) * tax_brackets[i][1]
else:
tax_owed += (tax_brackets[i+1][0] - tax_brackets[i][0]) * tax_brackets[i][1]
# Output the tax owed
print(f'Federal Income Taxes Owed: ${tax_owed}')
Fed_Income_Tax_Calc()
Solution Video: Challenge 29|PYTHON – Functional Taxation