Python税务计算器算法工作不正常

2024-10-02 22:35:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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笔记本环境中使用python3


Tags: theto算法forinttaxlevelsrates
3条回答

编辑:我终于弄明白了。结果发现,除了特殊人群,政府征收的税并不比收入多。谢谢大家的帮助!你知道吗

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)

你的工作大多是负数…你不检查他们的收入是否超过了特定的水平,所以当收入为100时,你向他们征收负税率(418400-100),以此类推。你知道吗

你想从第一个超过netincomeleft的数字开始,然后不要乘以-1!你知道吗

所以对于一个小收入者来说,“水平”应该从0或1开始,而不是从6开始。你知道吗

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

相关问题 更多 >