0

I have created a calculator that determines the tax for a given income. I use a for-loop to produce this output. I need to write a piece of code that extracts the top rate that applies to income, and I have been unable to do this. Right now, my calculator just returns the last tax rate in the loop, which for my print statement is .10. I need it to return .15 in this case.

#import TaxReturn class
from TaxReturn  import TaxReturn
#Define tax brackets for each filing status
class TaxCalculator:

   def __init__(self):
     self.brackets = {
        'single': (
            (0, 0.10),
            (8025, 0.15),
            (32550, 0.25),
            (78850, 0.28),
            (164550, 0.33),
            (357700, 0.35),
            (371815, 0.396)
            ),
        'married_jointly': (
            (0, 0.10),
            (16050, 0.15),
            (65100, 0.25),
            (131450, 0.28),
            (200300, 0.33),
            (357700, 0.35),
            (418292, 0.396)
            ),
        'married_separately': (
            (0, 0.10),
            (8025, 0.15),
            (32550, 0.25),
            (65725, 0.28),
            (100150, 0.33),
            (178850, 0.35),
            (209146, 0.396)
            ),
        'head_of_household': (
            (0, 0.10),
            (11450, 0.15),
            (43650, 0.25),
            (112650, 0.28),
            (182400, 0.33),
            (357700, 0.35),
            (395054, 0.396)
            )
                            }

  #calculate tax liability
   def TaxLiability (self, taxReturn):
      tax_liability = 0
      top_tax_rate = 0
      taxable_income = taxReturn.taxComp.taxable_inc
      for bracket in reversed(self.brackets[taxReturn.taxComp.filing_status]):
          if taxable_income > bracket[0]:
              tax_liability += (taxable_income - bracket[0]) * bracket[1]
              taxable_income -= taxable_income - bracket[0]             
              top_tax_rate = bracket[1]

      #round tax to two decimal places
      tax_liability = round(tax_liability, 2)
      return tax_liability, top_tax_rate

#assign name to TaxReturn class and update TaxReturn 
tr = TaxReturn()
tc = TaxCalculator()
tax_liability = tr.taxComp.inc_tax_before_credits 
top_tax_rate = tr.Misc.top_tax_rate
#test statements, output income tax before credits
tr.taxComp.filing_status = 'single'
tr.taxComp.taxable_inc = 25000
print('Unit Test for Tax Calulcator Module:')
print('///////////////////////////////////////////////////////////')
print("Single: ") 
print("Income tax before credits: " + str(tc.TaxLiability(tr)[0])) 
print("Top marginal rate: " + str(tc.TaxLiability(tr)[1]))
2
  • 3
    You only need to show the relevant part of the code. This is more likely to have people help you. Commented Dec 8, 2015 at 20:51
  • @ReutSharabani Sorry. Please see the method TaxLiability. I figured that showing someone the entire structure might help solve my problem. Commented Dec 8, 2015 at 20:54

1 Answer 1

1

Simply break once you've found the tax rate. This allows you to escape from the loop early, once your if condition has been met.

def TaxLiability (self, taxReturn):
  tax_liability = 0
  top_tax_rate = 0
  taxable_income = taxReturn.taxComp.taxable_inc
  for bracket in reversed(self.brackets[taxReturn.taxComp.filing_status]):
      if taxable_income > bracket[0]:
          tax_liability += (taxable_income - bracket[0]) * bracket[1]
          taxable_income -= taxable_income - bracket[0]             
          top_tax_rate = bracket[1]
          break

  #round tax to two decimal places
  tax_liability = round(tax_liability, 2)
  return tax_liability, top_tax_rate

You can also take a look at the documentation on break clauses within a loop.

Sign up to request clarification or add additional context in comments.

1 Comment

@Alex No problem! If that solved your problem, feel free to accept the answer (in general you should do that for all your questions, so that people know that you've gotten an answer). This also gives both you and the person who answered some additional reputation. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.