Tax Calculator - Python [closed] - python

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!

Related

Write a program that calculates net pay based on gross pay and tax breaks? (python) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 days ago.
Improve this question
I'm totally new to programming and haven't learned how to do this yet, so I would like to apologize for this simple question, but I'm not really sure where to start on this homework problem.
I would really appreciate any help.
Here's the problem:
"Write a program that calculates net pay based on gross pay and the following tax breaks:
salary <= $20,000: 0%
20,000 < salary <= $40,000: 10%
40,000 < salary <= $80,000: 15%
salary > $80,000: 20%
Note that for someone making $90,000, the first $20,000 is tax free, the next $20,000 falls in second tax break (10%) and so on."
This is what I did.
salary = input('What is the salary? ')
if salary <= 20000:
tax = salary
elif ((int(salary) > 20000) & (int(salary) <= 40000)):
tax = salary * 0.10
elif ((int(salary) > 40000) & (int(salary) <= 80000)):
tax = salary * 0.15
elif (int(salary) > 80000):
tax = salary * 0.2
if int(salary) >= 90000:
tax = 20000 * .10
print('Net pay is ' + (str(salary) + str(tax)))
This is what came up.
What is the salary? 20000
TypeError Traceback (most recent call last)
<ipython-input-32-203a42497b2d> in <module>
1 salary = input('What is the salary? ')
----> 2 if salary <= 20000:
3 tax = salary
4 elif ((int(salary) > 20000) & (int(salary) <= 40000)):
5 tax = salary * 0.10
>
TypeError: '<=' not supported between instances of 'str' and 'int'
Make sure to convert the input data to int before comparison - as simple as changing line 2 to if 'int(salary) <= 20000' - For starters

Calculating Percentage Python

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

Elif and range functions for progressive taxation calculation in percentage

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))

Struggling with the logic for a tip tax calculator with specific needs (python) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am writing a program for school with the following parameters:
Write a program that will calculate a XXX% tip and a 6% tax on a meal price. The user will enter the meal price and the program will calculate tip, tax, and the total. The total is the meal price plus the tip plus the tax. Your program will then display the values of tip, tax, and total.
The restaurant now wants to change the program so that the tip percent is based on the meal price. The new amounts are as follows:
Meal Price Range Tip Percent
.01 to 5.99 10%
6 to 12.00 13%
12.01 to 17.00 16%
17.01 to 25.00 19%
25.01 and more 22%
Here is my code so far:
def main():
#variables for calculating tip
a = .10
b = .13
c = .16
d = .19
e = .22
#variable for calculating tax
tax = .06
#variable for user input
user = 0.0
total = 0.0
#get user input
user = float(input("Please input the cost of the meal "))
if user > .01 and user < 5.99:
From here I have been unsuccessful in taking the user input and making my calculation of user*tax+user = total. That should give me my calculation but how do I implement it. This is on Python 3.6 IDLE.
After this line you want to execute your logic:
if user < .01 and user > 5.99:
(I assume this was supposed to be total is between $0.01 and $5.99)
So an example would look like this:
if user > .01 and user <= 5.99:
print("Total is: " + str((user*tax)*a))
elif user > 5.99 and user <= 12.00:
print("Total is: " + str((user*tax)*b))
etc
You could define a tip variable that you set based on meal price.
user = float(input("Please input the cost of the meal "))
tip = 0 # we'll overwrite this
if user > .01 and user < 5.99:
tip = 0.1
elif user > 5.99 and user < 12:
tip = 0.13
# etc, until...
else:
# at this point user should be >= 25.01
tip = 0.22
Then find the actual tip "price" and add it to total :
tip_price = user * tip
total += tip_price # note that you must still add tax and baseline price
You need:
import numpy as np
def price(x):
if x<=0: return 0
tax = 0.06
a = np.array([0.01, 6, 12.01, 17.01, 25.01])
b = np.array([10, 13, 16, 19, 22])/100
tip = dict(zip(a, b)).get(a[(x>=a).sum()-1] , 0)
return round(x * (1 + tax ) + tip,3)
for price 5, we have 5+(5*0.06) +0.1 = 5.4 for price=17, then 17+(17*0.06) +0.16=18.18 and for 30+(30*0.06) +0.22=32.02:
Now calling our price function:
price(5)
Out[745]: 5.4
price(17)
Out[746]: 18.18
price(30)
Out[747]: 32.02

Python tax calculator - algorithm is not working correctly

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())

Categories