Python Forum

Full Version: Simple problem. looking for an efficient way
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am absolutely new to python without any prior programming knowledge. my question is there an easy way to combine the categories given below in an efficient way. instead of writing the same thing over and over again can it be done more efficiently?

#Income Tax Calculation
print ("Category of the Tax Payers")
print ("---------------------------")
print ("1: Single")
print ("2: Married Filing Jointly")
print ("3: Married Filing Seperately")
print ("4: Head of Household")
category = eval(input("Please specifiy your category. e.g., 1,2,3,4: "))
income = eval(input("Please enter your income amount. e.g., 90000: "))
if category == 1:
    a = 8350
    b = 33950
    c = 82250
    d = 171550
    e = 372950
    if income <= a:
        tax_amount = income * 0.1
    elif income <= b:
        tax_amount = a * 0.1 + (income-a) * 0.15
    elif income <= c:
        tax_amount = a * 0.1 + b * 0.15 + (income-b) * 0.25
    elif income <= d:
        tax_amount = a * 0.1 + b * 0.15 + c * 0.25 + (income-c) * 0.28
    elif income <= e:
        tax_amount = a*0.1 + b*0.15 + c*0.25+ d*0.28+(income-d)*0.3
    else:
        tax_amount = a*0.1+b*0.15+c*0.25+d*0.28+e*0.3+(income-e)*0.35
    print ("Your tax amount is: ",tax_amount)
elif category == 2:
    a = 16700
    b = 33950
    c = 68525
    d = 104425
    e = 186475
    if income <= a:
        tax_amount = income * 0.1
    elif income <= b:
        tax_amount = a * 0.1 + (income-a) * 0.15
    elif income <= c:
        tax_amount = a * 0.1 + b * 0.15 + (income-b) * 0.25
    elif income <= d:
        tax_amount = a * 0.1 + b * 0.15 + c * 0.25 + (income-c) * 0.28
    elif income <= e:
        tax_amount = a*0.1 + b*0.15 + c*0.25+ d*0.28+(income-d)*0.3
    else:
        tax_amount = a*0.1+b*0.15+c*0.25+d*0.28+e*0.3+(income-e)*0.35
    print ("Your tax amount is: ",tax_amount)
elif category == 3:
    a = 8350
    b = 33950
    c = 68525
    d = 104425
    e = 186475
    if income <= a:
        tax_amount = income * 0.1
    elif income <= b:
        tax_amount = a * 0.1 + (income-a) * 0.15
    elif income <= c:
        tax_amount = a * 0.1 + b * 0.15 + (income-b) * 0.25
    elif income <= d:
        tax_amount = a * 0.1 + b * 0.15 + c * 0.25 + (income-c) * 0.28
    elif income <= e:
        tax_amount = a*0.1 + b*0.15 + c*0.25+ d*0.28+(income-d)*0.3
    else:
        tax_amount = a*0.1+b*0.15+c*0.25+d*0.28+e*0.3+(income-e)*0.35
    print ("Your tax amount is: ",tax_amount)
elif category == 4:
    a = 11950
    b = 45500
    c = 117450
    d = 190200
    e = 372950
    if income <= a:
        tax_amount = income * 0.1
    elif income <= b:
        tax_amount = a * 0.1 + (income-a) * 0.15
    elif income <= c:
        tax_amount = a * 0.1 + b * 0.15 + (income-b) * 0.25
    elif income <= d:
        tax_amount = a * 0.1 + b * 0.15 + c * 0.25 + (income-c) * 0.28
    elif income <= e:
        tax_amount = a*0.1 + b*0.15 + c*0.25+ d*0.28+(income-d)*0.3
    else:
        tax_amount = a*0.1+b*0.15+c*0.25+d*0.28+e*0.3+(income-e)*0.35
    print ("Your tax amount is: ",tax_amount)
else:
    print ("Please enter the valid category")
silverchick24 Wrote:imy question is there an easy way to combine the categories given below in an efficient way. instead of writing the same thing over and over again can it be done more efficiently?
Yes there is. It is even one of the most fundamental mottos in programming: DRY, which means don't repeat yourself. Every time you're writing the same thing over and over again, it can always be done more efficiently. The main tool for this is to write a function such as
def compute_tax(income, a, b, c, d, e):
    if income <= a:
        tax_amount = income * 0.1
    elif income <= b:
        tax_amount = a * 0.1 + (income-a) * 0.15
    elif income <= c:
        tax_amount = a * 0.1 + b * 0.15 + (income-b) * 0.25
    elif income <= d:
        tax_amount = a * 0.1 + b * 0.15 + c * 0.25 + (income-c) * 0.28
    elif income <= e:
        tax_amount = a*0.1 + b*0.15 + c*0.25+ d*0.28+(income-d)*0.3
    else:
        tax_amount = a*0.1+b*0.15+c*0.25+d*0.28+e*0.3+(income-e)*0.35
    return tax_amount
Then you can use the function in other parts of the code, for example
tax_amount = compute_tax(income, 16700, 33950, 68525, 104425, 186475)
To take 'write the same thing over and over again' even further one can avoid repetition altogether:

def compute_tax(income, brackets=None, rates=None):
    brackets = brackets or [16700, 33950, 68525, 104425, 186475, float('inf')]
    rates = rates or [0.1, 0.15, 0.25, 0.28, 0.3, 0.35]

    total = 0

    for i, (bracket, rate) in enumerate(zip(brackets, rates)):
        if bracket <= income:
            total += bracket * rate
        if income < brackets[i+1]:
            total += (income-bracket) * rates[i+1]
            break

    return total
or with different sets of brackets:
def calculate_tax(income, category):
    rate= [.1, .15, .25, .28, .3, .35]

    MaxIncome = [
        [8350, 33950, 82250, 171550, 372950],
        [16700, 33950, 68525, 104425, 186475],
        [8350, 33950, 68525, 104425, 186475],
        [11950, 45500, 117450, 190200, 372950]
    ]

    if income > MaxIncome[category-1][-1]:
        finalcalc = True

    tax_amount = 0.0
    finalcalc = False

    for n, max_income in enumerate(MaxIncome[category-1]):
        taxableamt = min(income, max_income)
        if taxableamt == 0:
            break
        tax_amount += rate[n] * taxableamt
        income = income - taxableamt
    if finalcalc:
        tax_amount += income * rate[-1]
    print (f"\nYour tax amount is: ${tax_amount}")

def tax_calculator():
    while True:
        print ("\nCategory of the Tax Payers")
        print ("---------------------------")
        print ("1: Single")
        print ("2: Married Filing Jointly")
        print ("3: Married Filing Seperately")
        print ("4: Head of Household")

        try:
            category = (input("Please specifiy your category. e.g., 1,2,3,4 (quit to end): "))
            if category == 'quit':
                break
            category = int(category)
            
            if category > 4 or category < 1:
                print('Invalid category')
                continue
            income = int(input("Please enter your income amount. e.g., 90000: "))
        except ValueError:
            print('Numbers only please')

        calculate_tax(income, category)


if __name__ == '__main__':
    tax_calculator()