1

So the basics of this, is that I am attempting to have a user input their yearly salary, then the program will run it through a set of if statements then return what tax bracket they are in, subtract the amount from their yearly salary and return what their gross income will be. The main problem is that it will run correctly for any amounts of $0 - $9,525 but once you jump up to the 12% and 22% it does not make it their. Like if you were to enter $100,000 it would just deduct 12% out of it when it should deduct 24%. Here is the Python code in its current form:

#Takes someone's yearly salary
yearlySalary = eval(input("Please enter your yearly salary: "))

if(yearlySalary < 9525):
    tax = yearlySalary * .10
    yearlySalary = yearlySalary-tax

elif(yearlySalary > 9526 && yearlySalary < 38700):
    tax = yearlySalary * .12
    yearlySalary = yearlySalary-tax

elif(range (38701, 82500)):
    tax = yearlySalary * .22
    yearlySalary = yearlySalary-tax

elif(range (82501, 157500)):
    tax = yearlySalary * .24
    yearlySalary = yearlySalary-tax

elif(range(157501, 200000)):
    tax = yearlySalary * .32
    yearlySalary = yearlySalary-tax

elif(range(200001, 500000)):
    tax = yearlySalary * .35
    yearlySalary = yearlySalary-tax

elif(yearlySalary > 500001):
    tax = yearlySalary * .37
    yearlySalary = yearlySalary-tax
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
tdelles
  • 11
  • 3
  • Your code as-written won't even run -- it has a SyntaxError. `&&` is spelled `and` in python. – Adam Smith Sep 18 '19 at 20:41
  • Tax brackets require calculations at each bracket up to and including the highest bracket you're in. If/elif/else only triggers for the first correct match (lowest bracket). – Andy Sep 18 '19 at 20:41
  • 1
    Possible duplicate of [Python's equivalent of && (logical-and) in an if-statement](https://stackoverflow.com/questions/2485466/pythons-equivalent-of-logical-and-in-an-if-statement) – DrCord Sep 18 '19 at 20:43
  • Use [Don’t Forget NumPy!](https://realpython.com/fast-flexible-pandas/#dont-forget-numpy) instead of all those `elif`. The method described, creates bins and prices to apply. – Trenton McKinney Sep 18 '19 at 20:48

4 Answers4

1

There's a few issues here about how tax brackets work; The brackets should be in the opposite order to catch the highest tax brackets first, and only the amount over the bracket should be taxed at that amount.

Also, for your example, elif(range (82501, 157500)): will not catch yearly_salary=100000 because the else statement is not asserting whether or not yearly_salary is actually within that range.

It is being caught in the 12% because the incorrect "AND" operator is used: use and instead of &&.

pcopp
  • 21
  • 4
0

You have to change your elif like:

elif(yearlySalary in range (38701, 82500)):

to know if your yearlySalary is in that range

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
0

Use np.digitize:

  • It allows for cleaner code
  • yearlySalary is binned, without messy code like:
    • yearlySalary > 9526 & yearlySalary < 38700
    • elif(yearlySalary in range (38701, 82500))
  • In the case of the following code, the bin sizes are as follows
    • 10% for 0 - 9525
    • 12% for 9526 - 38700
    • 22% for 38701 - 82500
    • 24% for 82501 - 157500
    • 32% for 157501 - 200000
    • 35% for 200001 - 500000
    • 37% for 500001 and up
  • .digitize works by finding the index of bins, within which, income falls. Given the index, find the corresponding value in tax_per.
  • bin_ is used as a variable because bin is an internal python function, and we don't overwrite internal functions.
import numpy as np

def tax_bracket(income: float) -> float:
    tax_per = np.array([.10, .12, .22, .24, .32, .35, .37])
    bin_ = np.digitize(income, bins=[9526, 38701, 82501, 157501, 200001, 500001])
    return income - income*(tax_per[bin_])


yearlySalary = float(input("Please enter your yearly salary: "))
print(tax_bracket(yearlySalary))

Output:

Please enter your yearly salary:  37524
33021.12

Using if - elif - else:

  • Arrange the bins from largest to smallest.
def old_way(yearlySalary):

    if yearlySalary > 500000:
        tax = yearlySalary * .37
        yearlySalary = yearlySalary-tax

    elif yearlySalary > 200000:
        tax = yearlySalary * .35
        yearlySalary = yearlySalary-tax

    elif yearlySalary > 157500:
        tax = yearlySalary * .32
        yearlySalary = yearlySalary-tax

    elif yearlySalary > 82500:
        tax = yearlySalary * .24
        yearlySalary = yearlySalary-tax

    elif yearlySalary > 38700:
        tax = yearlySalary * .22
        yearlySalary = yearlySalary-tax

    elif yearlySalary > 9525:
        tax = yearlySalary * .12
        yearlySalary = yearlySalary-tax

    else:
        tax = yearlySalary * .10
        yearlySalary = yearlySalary-tax

    return yearlySalary
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
0

Here it is a short program to compute the taxes.

boundaries = [0, 9525, 38700, 82500, 157000, 200000, 500000]
percentages = [ .10, .12,  .22, .24, .32, .35, .37]
sal = salary = 142000
bot_top = zip(boundaries, boundaries[1:]+[salary])
sal_in_brkt = (top-bot if sal>top else max(sal-bot,0) for bot, top in bot_top)
taxes = sum(pct*inbrkt for pct, inbrkt in zip(percentages, sal_in_brkt))
print(salary, taxes, salary-taxes) # ⇒ 142000 28369.5 113630.5

bot_top produces tuples with the bottom and top extremes of each taxation bracket (note that the top for the last bracket is taken equal to the salary itself).
sal_in_brkt produces the fractions of the salary that are comprised between top and bottom of each bracket.
taxes is the sum of the products of the fractions of salary comprised in a bracket times the corresponding percentage.


To generalize the procedure, a function make_taxes is defined below, that accepts as optional arguments a list of boundaries for tax brackets and a list of percentages that applies to the income inside a specific tax bracket.

The list of boundaries starts with the minimum income to which we apply taxation (in the example it is 0) and ends with the max amont over which taxation is constant. If we have N boundaries the number of brackets, taking into account the greater than max one, is still N so we assert that the lengths of the two lists must be equal.

The function make_taxes ultimately returns a function that returns the taxation for a given salary.

def make_taxes(boundaries=None, percentages=None):
    boundaries = boundaries or [0, 9525, 38700, 82500, 157000, 200000, 500000]
    percentages = percentages or [ .10, .12,  .22, .24, .32, .35, .37]
    assert len(boundaries) == len(percentages)
    def taxes(salary):
        s = salary
        pct_bot_top = zip(percentages, boundaries, boundaries[1:]+[s])
        return sum(p*(t-b if s>t else max(s-b, 0)) for p,b,t in pct_bot_top)
    return taxes

print(make_taxes()(142000)) # ⇒ 28369.5
gboffi
  • 22,939
  • 8
  • 54
  • 85