Python Forum
Simple problem. looking for an efficient way
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple problem. looking for an efficient way
#1
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")
Reply
#2
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)
Reply
#3
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A more efficient code titanif 2 459 Oct-17-2023, 02:07 PM
Last Post: deanhystad
  Cleaning my code to make it more efficient BSDevo 13 1,275 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  A simple problem, how best to solve it? SuchUmami 2 683 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
  Making a function more efficient CatorCanulis 9 1,750 Oct-06-2022, 07:47 AM
Last Post: DPaul
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,179 Jun-14-2022, 08:35 PM
Last Post: thesquid
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,755 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
Big Grin question about simple algorithm to my problem jamie_01 1 1,635 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  I there a more efficient way of printing ? Capitaine_Flam 7 3,415 Dec-01-2020, 10:37 AM
Last Post: buran
  A more efficient way of comparing two images in a python vukan 0 1,967 Mar-17-2020, 11:39 AM
Last Post: vukan
  simple login problem MMOToaster 2 2,367 Feb-25-2020, 09:28 AM
Last Post: MMOToaster

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020