【问题标题】:income tax calculation python所得税计算python
【发布时间】:2013-12-06 11:37:26
【问题描述】:

如何制作一个范围为 70000 及以上的 for 循环?我正在为所得税做一个 for 循环,当收入超过 70000 时,税率为 30%。我会做类似for income in range(income-70000) 的事情吗?

好吧,起初我开发了一个不使用循环的代码,它工作得很好,但后来我被告知我需要在我的代码中加入一个循环。这就是我所拥有的,但对我来说使用 for 循环是没有意义的。有人可以帮我吗?

def tax(income):

for income in range(10001):
    tax = 0
    for income in range(10002,30001):
        tax = income*(0.1) + tax
        for income in range(30002,70001):
            tax = income*(0.2) + tax
            for income in range(70002,100000):
                tax = income*(0.3) + tax
print (tax)

好的,所以我现在尝试了一个 while 循环,但它没有返回值。告诉我你的想法。我需要根据收入计算所得税。前10000美元是没有税的。下一个 20000 还有 10%。接下来的 40000 有 20%。 70000以上为30%。

def taxes(income):

income >= 0
while True:
    if income < 10000:
        tax = 0
    elif income > 10000 and income <= 30000:
        tax = (income-10000)*(0.1)
    elif income > 30000 and income <= 70000:
        tax = (income-30000)*(0.2) + 2000
    elif income > 70000:
        tax = (income - 70000)*(0.3) + 10000
return tax

【问题讨论】:

  • 所得税?你需要一个循环做什么?相乘。
  • 为什么不直接做if income &gt;= 7000?听起来您正试图将“收入超过 7000,征收 30% 税”的英文指令字面翻译成 Python,但 for 的实际作用可能会让您大吃一惊。
  • 也许他试图提出一个 Randian 观点,即高税收如何使经济陷入无限循环,永远不会产生任何结果。 ;)
  • 你的程序实际上应该做什么?
  • 我不知道任何python,但你的问题也不在于语言。您需要阅读有关条件的信息。你不需要所有的 FOR,只需要 1 并根据你的规则做 IFS。

标签: python for-loop iteration bisection


【解决方案1】:

让我们从数据(levelspcts)和一些值开始测试这种方法(测试工资包括括号边界)

In [1]: salaries = [5000, 10000, 20000, 30000, 50000, 70000, 90000] 
   ...: levels = [0, 10000, 30000, 70000] 
   ...: pcts = [0, .1, .2, .3]                  

我们有一个关于薪水的循环,我们重置了总所得税,然后我们对薪金括号执行一个循环(以bottomtop 表示括号)和相应的税收百分比。

如果salary-bot&lt;=0下一个括号我们没有收入,那么我们可以跳出内循环,否则我们将当前括号的pct应用到括号的总金额如果salary&gt;top否则工资中包含在括号中的部分并添加到总税款中。

In [2]: for salary in salaries: 
   ...:     tax = 0  
   ...:     for pct, bottom, top in zip(pcts, levels, levels[1:]+[salary]): 
   ...:         if salary - bottom <= 0 : break  
   ...:         tax += pct*((top-bottom) if salary>top else (salary-bottom)) 
   ...:     print(salary, tax)                                                            
5000 0
10000 0
20000 1000.0
30000 2000.0
50000 6000.0
70000 10000.0
90000 16000.0

使用生成器表达式和 sum 内置函数可能没有显式内部循环。

In [3]: for s in salaries: 
   ...:     bt = zip(levels, levels[1:]+[s]) 
   ...:     s_in_b = (t-b if s>t else s-b for b,t in bt if s-b>0) 
   ...:     tax = sum(p*s for p,s in zip(pcts, s_in_b)) 
   ...:     print(s, tax) 
   ...:                                                                                  
5000 0
10000 0
20000 1000.0
30000 2000.0
50000 6000.0
70000 10000.0
90000 16000.0

【讨论】:

    【解决方案2】:

    问:如何制作一个范围为 70000 及以上的 for 循环?

    答:使用itertools.count()方法:

    import itertools
    
    for amount in itertools.count(70000):
        print(amount * 0.30)
    

    问:我需要根据收入计算所得税。前10000美元是没有税的。接下来的20000还有10%。接下来的 40000 有 20%。 70000以上为30%。

    答:bisect module 非常适合在范围内进行查找:

    from bisect import bisect
    
    rates = [0, 10, 20, 30]   # 10%  20%  30%
    
    brackets = [10000,        # first 10,000
                30000,        # next  20,000
                70000]        # next  40,000
    
    base_tax = [0,            # 10,000 * 0%
                2000,         # 20,000 * 10%
                10000]        # 40,000 * 20% + 2,000
    
    def tax(income):
        i = bisect(brackets, income)
        if not i:
            return 0
        rate = rates[i]
        bracket = brackets[i-1]
        income_in_bracket = income - bracket
        tax_in_bracket = income_in_bracket * rate / 100
        total_tax = base_tax[i-1] + tax_in_bracket
        return total_tax
    

    【讨论】:

    【解决方案3】:

    我不知道任何 python,但你的问题也不在于语言。您需要阅读有关条件的信息。你不需要所有的 FOR,只需要 1 并根据你的规则做 IFS。

    【讨论】:

      【解决方案4】:

      您的两个函数都绝对计算所需的值。你需要这样的东西:

      import sys
      
      income = 100000
      taxes = [(10000, 0), (20000, 0.1), (40000, 0.2), (sys.maxint, 0.3)]
      
      billed_tax = 0
      for amount, tax in taxes:
          billed_amount = min(income, amount)
          billed_tax += billed_amount*tax
          income -= billed_amount
          if income <= 0: 
              break
      
      >>> billed_tax
      19000.0
      

      【讨论】:

      • 这个答案不正确。考虑通过循环的第二次迭代。金额是 20000,收入是 900000。所以 billed_amount 是 20000。所以 tax=.1 适用于 20000。但该税应仅适用于 20000 到 10000 之间的金额,即 10000。查看输入,您可以看到 19000 的税对于这个例子来说太高了,因为 60000 的税应该是 0.2,但其余的税应该比这个少得多,结果应该低于 19%。
      • @garyrob 你错了。如果您重新阅读该问题,则 10、20 和 40 是增量而不是实际步骤。步外增量的计算是显而易见的,是这种考虑的OOS。
      【解决方案5】:

      如果你真的必须循环,一种方法是分别将每个收入单位的税加起来:

      def calculate_tax(income):
          tax = 0
          brackets = {(10000,30000):0.1, ...}
          for bracket in brackets:
              if income > bracket[0]:
                  for _ in range(bracket[0], min(income, bracket[1])):
                      tax += brackets[bracket]
          return tax
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-26
        • 2012-11-19
        • 2022-11-17
        • 1970-01-01
        相关资源
        最近更新 更多