【问题标题】:Return First Iteration in a Loop返回循环中的第一个迭代
【发布时间】:2015-12-08 20:50:08
【问题描述】:

我创建了一个计算器,用于确定给定收入的税金。我使用一个 for 循环来产生这个输出。我需要编写一段代码来提取适用于收入的最高费率,但我一直无法做到这一点。现在,我的计算器只返回循环中的最后一个税率,对于我的打印语句,它是 0.10。在这种情况下,我需要它返回 0.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]))

【问题讨论】:

  • 只需要展示代码的相关部分即可。这更有可能让人们帮助你。
  • @ReutSharabani 对不起。请参阅方法 TaxLiability。我认为向某人展示整个结构可能有助于解决我的问题。

标签: python loops for-loop iteration


【解决方案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

您还可以查看循环中break 子句上的documentation。

【讨论】:

  • @Alex 没问题!如果这解决了您的问题,请随时accept the answer(通常您应该对所有问题都这样做,以便人们知道您已经得到了答案)。这也为您和回答的人提供了一些额外的声誉。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2015-06-23
  • 2023-02-24
  • 1970-01-01
  • 2013-04-25
  • 1970-01-01
  • 2018-05-14
相关资源
最近更新 更多