Im building a basic income tax calculator but can't figure out how to do all the necessary calculations
if income in range(120001, 180000):
tax = income + 29467
tax = income / 0.37
The given income if in this range needs to be $29,467 plus 37c for each $1 over $120,000 but i have no clue how to apply both calculations correctly
If I understood correctly, then try this option.
income = int(input('You income: '))
if income >= 120001 and income <= 180000:
tax = (29467 + (income - 120001) * 0.37)
print(f'income = {income} , tax = {tax} dollars')
Solution:
You income: 123500
income = 123500 , tax = 30761.63 dollars
why did you stated "tax" twice? then the first "tax" state will be wasted which means I shades with the latter "tax".
My translation of the question is this:
If income is in the range 120,000 to 180,000 then add a constant amount of 29,467 then an additional 0.37 for every unit over 120,000.
If that is the case then:
def calculate_tax(income):
return 29_467 + 0.37 * (income - 120_000) if 120_000 < income < 180_000 else 0
print(f'{calculate_tax(150_000):,.2f}')
Output:
40,567.00
You have income tax brackets.
0-120000. The rate is 29.467%
120001-180000 the rate is 37%. Based on your data
So for an income of 150000. The income tax is 120000*0.29467 + (150000-120000)*0.37
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Question:
Calculate income tax for the given income by adhering to the below rules:
Taxable Income
Rate (%)
First $10,000
0
Next $10,000
10%
The remaining
20%
Solution (that I did):
income = int(input('What is your income? '))
if income <= 10_000:`
print('You have zero tax')
elif 10_000 < income <= 20_000:
income_2 = (income - 10_000)*0.1
print(f"Your tax amount to be paid is ${income_2}")
elif income > 20_000:
income_3_1 = ((income - 10_000)*0.1)
income_3_2 = ((income - 20_000)*0.2)
income_3 = income_3_1 + income_3_2
print(f"Your tax amount to be paid is ${income_3}")```
What mistake did I make? Where did my algo go wrong?
Simple routine with test:
for income in [5000, 15000, 25000]:
if income <= 10_000:
tax = 0
elif income <= 20_000:
tax = (income - 10_000) * 0.1 # 10% on income above 10K
else:
tax = (income-20_000) * 0.2 + 10_000*0.1 # 20% on income above 20K, plus tax on 10K of income below 20K
print(f"For income {income}, You owe {tax} dollars in tax!" )
Output
For income 5000, You owe 0 dollars in tax!
For income 15000, You owe 500.0 dollars in tax!
For income 25000, You owe 2000.0 dollars in tax!
Can anyone help me writing the split_check() function?.
The problem statement is:
Write a split_check function that returns the amount that each diner
must pay to cover the cost of the meal. The function has 4 parameters:
bill: The amount of the bill. people: The number of diners to split
the bill between. tax_percentage: The extra tax percentage to add to
the bill. tip_percentage: The extra tip percentage to add to the bill.
The tax or tip percentages are optional and may not be given when
calling split_check. Use default parameter values of 0.15 (15%) for
tip_percentage, and 0.09 (9%) for tax_percentage
I need to calculate the amount of tip and tax, add to the bill total, then divide by the number of diners.
bill = float(input())
people = int(input())
#Cost per diner at the default tax and tip percentages
print('Cost per diner:', split_check(bill, people))
bill = float(input())
people = int(input())
new_tax_percentage = float(input())
new_tip_percentage = float(input())
# Cost per diner at different tax and tip percentages
print('Cost per diner:', split_check(bill, people, new_tax_percentage, new_tip_percentage))
You can see only bill and people are required, so you should add default values to your arguments:
def split_check(bill, people, tax = 0.09, tip = 0.15)
That means that if only two arguments are given, like in the first case, the tax and tip percentages will be 9% and 15% respectively. You should add to bill the amount bill*tax and bill*tip. In the end, you'll divide the bill by the number of people and return it.
So we have this:
def split_check(bill, people, tax = 0.09, tip = 0.15):
taxes = bill * tax
tips = bill * tip
return (bill + taxes + tips) / people
You can also check if people is not smaller or equal to 0, if the arguments are numbers and not strings for example, and if tax and tip are between 0 and 1, etc. using a try/except block.
Heres what I did:
I named the parameters with all default amounts
named new local variables to get new amounts
returned the per_person calculation
def split_check(bill=0, people=0, tax_percentage=0.09, tip_percentage=0.15):
new_check = (bill * tax_percentage) + bill
new_tip = bill * tip_percentage
per_person = (new_check + new_tip) / people
return per_person
bill = float(input())
people = int(input())
# Cost per diner at the default tax and tip percentages
print('Cost per diner:', split_check(bill, people))
bill = float(input())
people = int(input())
new_tax_percentage = float(input())
new_tip_percentage = float(input())
# Cost per diner at different tax and tip percentages
print('Cost per diner:', split_check(bill, people, new_tax_percentage, new_tip_percentage))
this works for me
def split_check(bill=0.0, people=0, tax_percentage=0.09, tip_percentage=0.15):
bill_per_diner = ((bill + ((tax_percentage * bill) + (tip_percentage * bill))) / people)
return bill_per_diner
Taking all of the comments into account to understand the problem completely, here's an answer that produces the right result:
def split_check(bill, people, tax = 0.15, tip = 0.09):
tax = bill * tax
tip = bill * tip
return (bill + tax + tip)/people
print('Cost per diner:', split_check(25, 2))
Or more concisely:
def split_check(bill, people, tax = 0.15, tip = 0.09):
return bill * (1.0 + tax + tip) / people
print('Cost per diner:', split_check(25, 2))
Result:
Cost per diner: 15.5
I’m writing a simple program and I’m using .format() to round the number to 2 d.p.
State = 0.05
County = 0.025
Purchase = float(input(‘amount of purchase: ‘))
State_tax = purchase * state
County_tax = purchase * county
Total_tax = state_tax + county_tax
Total = purchase + total_tax
Print(‘amount: ‘ + ‘{:.2f}’.format(purchase))
Print(‘state tax: ‘ + ‘{:.2f}.format(state_tax))
Print(‘county tax: ‘ + ‘{:.2f}.format(county_tax))
Print(‘total tax: ‘ + ‘{:.2f}.format(total_tax))
Print(‘total sale: ‘ + ‘{:.2f}.format(total))
To test it I inputted 11. However, the total doesn’t add up correctly to the tax. The total tax is 0.83 but the total is 11.82. It doesn’t round 11.825 to 11.83. How could I fix this?
Because you are calculating tax total without formatting
Try this one for tax as you shown in print
total_tax = float('{:.2f}'.format(state_tax)) + float('{:.2f}'.format(county_tax))
This fix fix your total
Python usually round to min value if fraction value is less than or equal to 5, It rounds up to max value when the fraction value is above 5.
You can even try this to round the number
print("amount: {}".format(round(purchase, 2)))
To round the amount to 2 digits
I am creating a program where user puts yearly income as an input and the income taxation rate would scale according to percentage thresholds (see picture at the end of this post for income range vs. percentage thresholds). For example from 100,000 euro income 50% would be paid from income exceeding 60001 (as according in the table below). 40% betweeen 45000-60000, 20% between 15000-30000 and 10% between 0-10000. Equation should go something like 0.5* 40 000 + 0.4 * 150 00 + 0.3 * 15 000 + 0.2 * 15 000 + 0.1 * 10000 = 35 000.
Outputs should be something like:
Give your yearly income: 100000 You pay taxes 35000.0 euro. Your taxation percent is: 35.0 %.
Give your yearly income: 54000 You pay taxes 12600.0 euroa. Your taxation percent is: 23.333333333333332 %.
This is what I've tried:
yearly = int(input("Tell your yearly income:"))
one = 0.1
two = 0.2
three = 0.3
four = 0.4
five = 0.5
if yearly in range(1,15000):
print("You pay taxes in total:",yearly*one)
if yearly in range(15001,30000):
print("You pay taxes in total:",yearly*two)
if yearly in range(30001,45000):
print("You pay taxes in total:",yearly*three)
if yearly in range(45001,60000):
print("You pay taxes in total:",yearly*four)
if yearly > 60000:
print("You pay taxes in total:",yearly*five)
The taxation percent doesn't scale like I'd like it to. I assumption is that this should get done by elif and range functions. Maybe min and max values of percentages would help, but I don't know how to turn outputs into percentages. Please note that I might have some comprehension problems here, math is not my strongest gamem but I did my absolute best to describe the problem...
Here is a table of income count vs. taxation percentage used.
Income vs. taxation percentage
Optimized way :
Here is a more-optimized way to do it (see at bottom for a non-optimized way with enumeration). I store the percents and thresholds in lists, and taxes is incremented during a loop :
yearly = int(input("Tell your yearly income:"))
percents = [0.1, 0.2, 0.3, 0.4, 0.5]
thresholds = [0, 15000, 30000, 45000, 60000]
taxes = 0
for i in range(len(percents)):
if i == len(thresholds) - 1 or yearly < thresholds[i+1]:
taxes += (yearly - thresholds[i]) * percents[i]
break
else:
taxes += (thresholds[i+1] - thresholds[i]) * percents[i]
print("You pay {} taxes".format(taxes))
print("Your taxation percent is: {}%".format(taxes/yearly*100))
Examples of outputs :
# Test 1
Tell your yearly income:100000
You pay 35000.0 taxes
Your taxation percent is: 35.0%
# Test 2
Tell your yearly income:54000
You pay 12600.0 taxes
Your taxation percent is: 23.333333333333332%
Non-optimized way :
Here the key is to have a variable temp (initialized with temp = yearly that decreases when you apply the taxes :
yearly = int(input("Tell your yearly income:"))
one = 0.1
two = 0.2
three = 0.3
four = 0.4
five = 0.5
diff = 15000
temp = yearly
taxes = 0
if yearly in range(1,15000):
taxes += temp * one
else:
taxes += diff * one
temp -= diff
if yearly in range(15001,30000):
taxes += temp * two
else:
taxes += diff * two
temp -= diff
if yearly in range(30001,45000):
taxes += temp * three
else:
taxes += diff * three
temp -= diff
if yearly in range(45001,60000):
taxes += temp * four
else:
taxes += diff * four
temp -= diff
if yearly > 60000:
taxes += temp * five
print("You pay {} taxes".format(taxes))
print("Your taxation percent is: {}%".format(taxes/yearly*100))
I'm very new to python so I apologize if this question is simple. I am trying to write an algorithm that calculates the difference in rates between 2017 and 2018 based on a user-inputted salary. I've gotten to the point where the algorithm does calculate a tax rate, however it seems to do it backwards, i.e. the lower the inputted income, the higher the tax owed, something that the government, for all its flaws, generally doesn't do. I've tried different things for the algorithm but I'm still not sure where I'm going wrong. Any ideas or advice would be greatly appreciated.
Thanks!
# of tax brackets
levels = 6
#2017 tax rates
rates2017 = [0, 10, 15, 25, 28, 33, 35]
#2018 tax rates
rates2018 = []
#2017 income tax thresholds
incomes2017 = [0, 9325, 37950, 91900, 191650, 416700, 418400]
# take in a value for net income and assign it to int
netincome = int(input('Please input an integer for income: '))
#initialize the variables used
tax_owed = 0
taxable_income = 0
netincomeleft = netincome - 6500
i = levels
#while loop calculates the income tax
while i >= 0:
taxable_income = netincomeleft - incomes2017[i]
tax_owed += taxable_income * (rates2017[i]/100)
netincomeleft = incomes2017[i]
i -= 1
#multiply tax owed by -1 to get a positive int for clarity
taxes_owed = tax_owed * -1
# print out the 2017 tax owed
print('tax owed on $', netincome, 'after standard deduction is ', taxes_owed)
*for the sake of clarity, I'm using Python 3 in a Jupyter notebook environment
You are working mostly with negative numbers....you don't check whether their income EXCEEDS the specific level, so with an income of 100 you charge them NEGATIVE tax rate of (418400 - 100) and so on.
You want to start your level at the first number EXCEEDING netincomeleft, and then not multiply by -1!
So for a small income "level" should start at 0 or 1, not at 6.
Edit: I did figure this out eventually. Turns out the government does not collect more tax than income, except for special people. Thanks everyone for your help!
levels = 6
rates2017 = [0, 10, 15, 25, 28, 33, 35]
rates2018 = []
incomes2017 = [0, 9325, 37950, 91900, 191650, 416700, 418400]
netincome = int(input('Please input an integer for income: '))
tax_owed = 0
taxable_income = 0
standard_deduction = 6500
netincomeleft = netincome - standard_deduction
i = 0
while levels >= 0 and taxable_income >=0 and netincomeleft >= 0:
if (netincomeleft - incomes2017[i]) < 0:
taxable_income = netincome - incomes2017[i-1] - standard_deduction
else:
taxable_income = netincomeleft - incomes2017[i]
tax_owed += (taxable_income * (rates2017[i]/100))
netincomeleft = netincomeleft - incomes2017[i]
i += 1
levels -= 1
taxes_owed = tax_owed
print('tax owed on $', netincome, 'after standard deduction is ', taxes_owed)
def USA():
tax=0
if salary<=9700:
tax=9700*0.1
elif salary<=39475:
tax=970+(salary-9700)*0.12
elif salary<=84200:
tax=4543+(salary-39475)*0.22
elif salary<=160725:
tax=14382.5+(salary-84200)*0.24
elif salary<=204100:
tax=32748.5+(salary-160725)*0.32
elif salary<=510300:
tax=139918.5+(salary-204100)*0.35
else:
tax=328729.87+(salary-510300)*0.37
return ('tax owed on $', salary, 'after standard deduction is ', tax, 'and your netincome is ', (salary-tax) )
salary=int(input('Please input an integer for income: '))
print(USA())