我正在尝试解决有关彩票奖金的问题。众所周知,在一个人赢得彩票金额后,他们并不总是拿全额。他们在不同的括号中征税,我正在尝试创建一个python程序,告诉我们我们对winnngs征税的金额。
在评估任何税项之前,获胜者将获得6300美元的标准扣除额和4000美元的个人免税额。因此,在我们找出征税金额之前,我们通过公式
找到应税金额taxablewinnings = winnings-(标准扣除+个人豁免)
之后,获胜金额将在这些金额括号中分类。
$ 0到$ 9,225 ---- 10%
$ 9,225至$ 37,450 ---- 15%
$ 37,450至$ 90,750 ---- 25%
$ 90,750至$ 189,300 ---- 28%
$ 189,300至$ 411,500 ---- 33%
$ 411,500至$ 413,200 ---- 35%
$ 413,200 + ---- 39.6%
例如。如果一个人赢得54000美元 taxablewinnings = $ 54000- $ 6300- $ 4000 = $ 43,400是将征税的金额。其中:
$ 9225的税率为10%= $ 922.50 还没有征税34,475美元
28,225美元的税率为15%= 4233.75美元 还没有征税6,250美元$ 6,250的税率为25%= $ 1,562.50
总欠款= 922.5 + 4233.75 + 1562.5 = $ 6718.75(或$ 6,719四舍五入)
这是我的代码。
winnings = float(input("Please enter your Winning amount"))
tax = 0
standardDeduction = 6300
personalExemption = 4000
taxablewinnings = winnings - (standardDeduction+personalExemption)
if taxablewinnings > 0 and taxablewinnings <= 9225:
rate1 = 9225*0.10
remainder1 = taxablewinnings-9225
if taxablewinnings > 9225 and taxablewinnings <= 37450:
rate2 = remainder1*0.15
remainder2 = taxablewinnings-37450
if taxablewinnings > 37450 and taxablewinnings <= 90750:
rate3 = remainder2*0.25
remainder3 = taxablewinnings-90750
if taxablewinnings > 90750 and taxablewinnings <= 189300:
rate4 = remainder3*0.28
remainder4 = taxablewinnings-189300
if taxablewinnings > 189300 and taxablewinnings <= 411500:
rate5 = remainder4*0.33
remainder5 = taxablewinnings-411500
if taxablewinnings > 411500 and taxablewinnings <= 413200:
rate6 = remainder5*0.33
remainder6 = taxablewinnings-413200
if taxablewinnings > 413200:
rate7 = remainder6*0.396
else:
print("Invalid winnings input")
if(winnings > 0):
print("Your tax is: $%f" % tax)
我收到错误
rate3 = remainder2 * 0.25 NameError:name&#39; remainder2&#39;未定义
答案 0 :(得分:3)
如果taxablewinnings大于37450,则此错误将始终发生,因为如果taxablewinnings在37450到92250范围内,则仅定义remaining2。
答案 1 :(得分:1)
我重写了你程序的某些部分:
winnings = int(raw_input("Amount won: "))
STD_DEDUCTION = 6300
PERSONAL_EXEMPTION = 4000
TAX_BRACKETS = [(0, 0), (9225, .1), (37450, .15), (90750, .25),
(189300, .28), (411500, .33), (413200, .35)]
taxable = winnings - (STD_DEDUCTION + PERSONAL_EXEMPTION)
tax = 0
for i in xrange(1, len(TAX_BRACKETS)):
value = TAX_BRACKETS[i][0] - TAX_BRACKETS[i-1][0]
percent = TAX_BRACKETS[i][1]
amt_to_tax = taxable if taxable < value else value
tax += amt_to_tax * percent
taxable -= amt_to_tax
tax += taxable * .396
print "Winnings: {}\nTax: {}\nWinnings after taxes: {}".format(
winnings, tax, winnings - tax)
我认为这个解决方案比您的解决方案更强大,但它仍然确实包含了您的代码精神。
答案 2 :(得分:0)
原始问题的答案是,符号未定义,因为定义出现在未采用的'if'语句中,例如,在此块中:
if taxablewinnings > 9225 and taxablewinnings <= 37450:
rate2 = remainder1*0.15
remainder2 = taxablewinnings - 37450
如果您的奖金少于9225,则代码remainder2 = taxablewinnings - 37450
永远不会执行。如果您的奖金大于37450,则该块永远不会被执行,因为它们太高了。你在这里有一个逻辑错误。 (并且贯穿始终。)
另外,您如何处理纳税金额不是全额的情况?
例如,如果我的应税奖金为40000,则肯定符合taxablewinnings > 9225
。但40000与taxablewinnings <= 37450
不匹配。因此,您将我所有的奖金强制纳入更高的税级。
或者你会这么做,除了你正在寻找一个从未被初始化的remainder2
,因为你完全跳过了上面的块。
相反,您想要从奖金中略微咬一口,即使它们高于税率的上限。因此,如果40000是应纳税的奖金,那么将其中的一部分纳税为0.15,然后再继续。
taxes_due = 0.0
if taxablewinnings < 0:
taxablewinnings = 0
if taxablewinnings > 0:
rate = 0.10
lower_limit = 0
upper_limit = 9225
if taxablewinnings <= upper_limit:
taxes_due += rate * (taxablewinnings - lower_limit)
else:
taxes_due += rate * (upper_limit - lower_limit)
if taxablewinnings > 9225:
rate = 0.15
lower_limit = 9225
upper_limit = 37450
if taxablewinnings <= upper_limit
taxes_due += rate * (taxablewinnings - lower_limit)
else:
taxes_due += rate * (upper_limit - lower_limit)
你可以(我希望)从这里看到模式。显然,最后一个括号不会有上限,所以它会更简单一些。祝你好运。
答案 3 :(得分:0)
按照你的解释,因为我来自美国以外,那么这可能是你想要的
standardDeduction = 6300
personalExemption = 4000
tax_brackets = [ ( 0, 9225, 0.10),
( 9225, 37450, 0.15),
( 37450, 90750, 0.25),
( 90750, 189300, 0.28),
(189300, 411500, 0.33),
(411500, 413200, 0.35),
(413200, None, 0.396) ]
def calculate_tax(total):
no_tax = standardDeduction + personalExemption
taxable = total - no_tax
total_tax = 0.0
for min_val, max_val, tax in tax_brackets :
if taxable <= 0:
break
amount = (max_val - min_val) if max_val is not None else min_val
if taxable <= amount:
amount = taxable
total_tax += amount * tax
taxable -= amount
return total_tax
测试
>>> calculate_tax(54000)
6718.75
>>>
而不是像你的代码一样一个 只使用 ,最好使它成为一个函数,因此可以多次使用,现在关于代码,第一部分是自解释的,现在是for循环的有趣部分:这里我们迭代tax_brackets而我们有税收或直到我们用完括号,因为你解释我们采取等于括号边界之间的差异,但如果该金额超过应税金额,我们采取剩余的,然后应用当前括号的税并减去使用量。
编辑
上一个函数可以用if
表示如下
standardDeduction = 6300
personalExemption = 4000
no_tax = standardDeduction + personalExemption
total = float(input("Please enter your Winning amount: "))
taxable = total - no_tax
total_tax = 0.0
if taxable > 0: # brackets 1
amount = 9225 # - 0
if taxable <= amount:
amount = taxable
total_tax += amount * 0.1
taxable -= amount
if taxable > 0: # brackets 2
amount = 37450 - 9225
if taxable <= amount:
amount = taxable
total_tax += amount * 0.15
taxable -= amount
if taxable > 0: # brackets 3
amount = 90750 - 37450
if taxable <= amount:
amount = taxable
total_tax += amount * 0.25
taxable -= amount
if taxable > 0: # brackets 4
amount = 189300 - 90750
if taxable <= amount:
amount = taxable
total_tax += amount * 0.28
taxable -= amount
if taxable > 0: # brackets 5
amount = 411500 - 189300
if taxable <= amount:
amount = taxable
total_tax += amount * 0.33
taxable -= amount
if taxable > 0: # brackets 6
amount = 413200 - 411500
if taxable <= amount:
amount = taxable
total_tax += amount * 0.35
taxable -= amount
if taxable > 0: # brackets 7
amount = 413200
if taxable <= amount:
amount = taxable
total_tax += amount * 0.396
taxable -= amount
if total > 0:
print("you win",total,"and you have to paid",total_tax,"in tax")
else:
print("Invalid winnings input")
(这是函数的逐步字面翻译)
测试
Please enter your Winning amount: 54000
you win 54000.0 and you have to paid 6718.75 in tax
>>>