2015-12-08 86 views
0

我創建了一個計算器,該計算器確定給定收入的稅額。我使用for-loop來產生這個輸出。我需要編寫一段代碼來提取適用於收入的最高費率,但我一直無法做到這一點。現在,我的計算器只是返回循環中的最後一個稅率,這對我的印刷聲明是.10。在這種情況下,我需要它返回.15。返回循環中的第一次迭代

#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])) 
+3

您只需顯示代碼的相關部分。這更有可能讓人們幫助你。 –

+0

@ReutSharabani對不起。請參閱TaxLiability方法。我認爲向某人展示整個結構可能有助於解決我的問題。 –

回答

1

只要break一旦你找到了稅率。這可以讓您在您的if條件滿足後儘早從循環中退出。

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 

您還可以看看一個循環內的documentationbreak條款。

+0

哦,哇,謝謝,這真的很容易@nbrooks –

+0

@Alex沒問題!如果這解決了你的問題,請隨時[接受答案](http://meta.stackexchange.com/a/5235)(一般來說,你應該爲你所有的問題做到這一點,以便讓人們知道你已經得到了一個答案)。這也給了你和那些回答一些額外聲譽的人。謝謝! – nbrooks