gpt4 book ai didi

Python 税收计算器 - 算法无法正常工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:51:47 25 4
gpt4 key购买 nike

我是 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 notebook 环境中使用 Python 3

最佳答案

您主要处理负数....您不检查他们的收入是否超过特定水平,因此如果收入为 100,您向他们收取 (418400 - 100) 的负税率,依此类推。

您想从第一个超过 netcomeLeft 的数字开始您的关卡,然后不乘以 -1!

因此对于小收入来说,“水平”应该从 0 或 1 开始,而不是从 6 开始。

关于Python 税收计算器 - 算法无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48709825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com