我对python很新,所以如果这个问题很简单,我会道歉。我正在尝试编写一种算法,根据用户输入的工资计算2017年和2018年之间的差异。我已经达到了算法计算税率的程度,但它似乎倒退了,即输入的收入越低,所欠的税越高,政府一般来说,所有的缺陷都是如此不行。我已经为算法尝试了不同的东西,但我仍然不确定我哪里出错了。任何想法或建议将不胜感激。 谢谢!
# 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)
*为了清楚起见,我在Jupyter笔记本环境中使用Python 3
答案 0 :(得分:2)
你的工作主要是负数....你不检查他们的收入是否超过了特定的水平,所以收入为100你就会向他们收取负债税率(418400 - 100)等等
你想在第一个数字超过netincomeleft时开始你的等级,然后不要乘以-1!
所以对于一个小的收入"水平"应该从0或1开始,而不是从6开始。
答案 1 :(得分:0)
编辑:我最终确实弄明白了。事实证明,除了特殊的人,政府不会收取更多的税收而不是收入。谢谢大家的帮助!
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)
答案 2 :(得分:0)
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())