Reputation: 13
I am trying to solve a question regarding lottery winnings. As we all know that after a person wins a lottery amount, they don't always take the full amount. They get taxed in different brackets and I am trying to create a python program which tells us the amount that we are taxed on our winnngs.
Before any taxes are assessed, the winner is given a standard deduction of $6300 and a personal exemption of $4000. So before we find out how much amount is taxed, we find the taxable amount by the formula
taxablewinnings = winnings-(Standard Deduction+Personal Exemption)
After that the winning amount is categorized in these amount brackets.
$0 to $9,225----10%
$9,225 to $37,450----15%
$37,450 to $90,750----25%
$90,750 to $189,300----28%
$189,300 to $411,500----33%
$411,500 to $413,200----35%
$413,200+ ----39.6%
For example. If a person wins $54000 taxablewinnings = $54000-$6300-$4000 =$43,700 is the amount which will be taxed. Of that amount:
$9225 is taxed at 10% = $922.50 leaving $34,475 not yet taxed
$28,225 is taxed at 15% = $4233.75 leaving $6,250 not yet taxed
$6,250 is taxed at 25% = $1,562.50
Total owed = 922.5 + 4233.75 + 1562.5 = $6718.75 (or $6,719 rounded)
Here is my code.
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)
I am getting the error
rate3 = remainder2*0.25 NameError: name 'remainder2' is not defined
Upvotes: 0
Views: 306
Reputation: 8520
going by your explanations, because I am from outside of usa, then this may be what you want
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
test
>>> calculate_tax(54000)
6718.75
>>>
Instead of doing it one use only as in your code, is best to make it a function so it can be used multiples times, now about the code, the first part is self explanatory, now the interesting part the for loop: here we iterate over the tax_brackets while we have something to tax or until we run out of brackets, as you explain we take a amount equal to the difference between the boundaries of the bracket but if that amount excede the taxable money we take the leftover instead, then apply the tax of the current bracket and subtract used amount.
EDIT
The previous function can be expressed in terms of if
as follow
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")
(this is an almost literal translation of the function step by step)
test
Please enter your Winning amount: 54000
you win 54000.0 and you have to paid 6718.75 in tax
>>>
Upvotes: 0
Reputation: 15310
The answer to your original question is, the symbol is undefined because the definition appears inside an un-taken 'if' statement, for example, in this block:
if taxablewinnings > 9225 and taxablewinnings <= 37450:
rate2 = remainder1*0.15
remainder2 = taxablewinnings - 37450
If your winnings were less than 9225, the code remainder2 = taxablewinnings - 37450
is never executed. And if your winnings are greater than 37450, the block is never executed because they are too high. You have a logic error here. (And throughout.)
Also, how do you handle the case where the amount to be taxed is NOT the full amount?
For example, if my taxable winnings are 40000, that certainly qualifies as being taxablewinnings > 9225
. But 40000 does not match taxablewinnings <= 37450
. So you force all my winnings into the higher tax bracket.
Or you would, except that you are looking for a remainder2
that has never been initialized, because you completely skipped the block above.
Instead, you want to take a little bite out of the winnings, even when they are higher than the upper limit of the tax bracket. So if 40000 is the taxable winnings, then tax some amount of it at 0.15, and then go on.
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)
You can (I hope) see the pattern from here. Obviously, the last bracket won't have an upper limit, so it will be a little simpler. Good luck.
Upvotes: 0
Reputation: 6607
I rewrote certain parts of your program:
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)
I think that this solution is a little bit more robust than your solution, but it still really does encompass the spirit of your code.
Upvotes: 1
Reputation: 56
If taxablewinnings is greater than 37450, this error will always occur because remainder2 is getting defined ONLY if taxablewinnings is in the 37450 to 92250 range.
Upvotes: 3