4

如何制作一个范围为 70000 及以上的 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
4

5 回答 5

17

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

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

import itertools

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

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

A: bisect模块非常适合在范围内进行查找:

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
于 2013-11-21T19:47:34.887 回答
1

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

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
于 2013-11-21T20:42:39.477 回答
1

Let's start with the data (levels and pcts) and some values to test this approach (the test salaries comprises the bracket boundaries)

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

We have a loop on the salaries, we reset the total income tax and next we perform a loop on the salary brackets (expressed in terms of the bottom and the top of each bracket) and the corresponding percentages of taxation.

If salary-bot<=0 we have no income in the next brackets, so we can break out of the inner loop, otherwise we apply the pct for the current bracket to the total amount of the bracket if salary>top otherwise to the part of salary comprised in bracket and add to the total tax.

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

It is possible to have no explicit inner loop using generator expressions and the sum builtin.

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
于 2019-10-01T14:30:12.997 回答
0

您的两个函数都绝对不会计算所需的值。你需要类似的东西:

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
于 2013-11-21T20:52:10.927 回答
-1

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

于 2013-11-21T20:57:07.853 回答