Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 所得税计算_Python_For Loop_Iteration_Bisection - Fatal编程技术网

Python 所得税计算

Python 所得税计算,python,for-loop,iteration,bisection,Python,For Loop,Iteration,Bisection,如何制作范围为70000及以上的for循环?我正在为所得税做一个for循环,当收入超过70000时,要缴纳30%的税。我会对范围内的收入(收入-70000)执行类似于的操作吗 In [2]: for salary in salaries: ...: tax = 0 ...: for pct, bottom, top in zip(pcts, levels, levels[1:]+[salary]): ...: if salary - bo

如何制作范围为70000及以上的for循环?我正在为所得税做一个for循环,当收入超过70000时,要缴纳30%的税。我会对范围内的收入(收入-70000)执行类似于
的操作吗

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
好的,起初我开发了一个不使用循环的代码,它工作得很好,但是后来我被告知我需要在代码中加入一个循环。这就是我所拥有的,但使用for循环对我来说毫无意义。有人能帮我吗

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
def税(收入):

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

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
def税(收入):

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
收入>=0
尽管如此:
如果收入<10000:
税=0
elif收入>10000,收入30000,收入70000:
税收=(收入-70000)*(0.3)+10000
退税

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

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
A:使用以下方法:

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
Q:我需要根据收入计算所得税。前10000美元是免税的。接下来是10%。接下来是20%。70000以上为30%。

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
A:非常适合在范围内进行查找:

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
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

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

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
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

两个函数都不计算所需的值。你需要像这样的东西:

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
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
导入系统 收入=100000 税收=[(10000,0)、(20000,0.1)、(40000,0.2)、(sys.maxint,0.3)] 账单税=0 对于金额,税中的税: 账单金额=最低(收入、金额) 开票税费+=开票金额*税费 收入-=账单金额 如果收入>>已开票的税费 19000
我不懂python,但你的问题也不是语言。你需要了解条件句。你不需要所有这些,只要1,并根据你的规则做假设

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

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
我们在工资上有一个循环,我们重置了总所得税,接下来我们在工资等级(以每个等级的
底部
顶部
表示)和相应的税收百分比上执行一个循环

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
如果
salary bottop
,则将包含在括号中的工资部分添加到总税费中

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

所得税你需要一个环做什么?如果收入>=7000,为什么不直接做呢?听起来你正试图将英文指令“对于收入超过7000的人,征收30%的税”翻译成Python,但
对于
的实际效果可能会让你感到惊讶。也许他是在试图提出一个随机观点,即顶层的高税收如何将经济变成一个无限循环,永远不会产生任何结果你的程序实际上应该做什么?我不知道任何python,但你的问题也不是语言。你需要了解条件句。你不需要所有这些,只要1,根据你的规则做假设。这个答案是错误的。考虑通过循环的第二次迭代。金额为20000,收入为900000。所以账单金额是20000。因此,tax=.1适用于20000。但该税只适用于20000至10000之间的金额,即10000。看一下输入,你可以看到19000的税对于这个例子来说太高了,因为60000应该在.2征税,但其余的应该少征税,结果应该少于19%。@garyrob你错了。如果你重新阅读问题,10、20和40不是实际步骤。分步递增的计算是显而易见的,这是考虑到的OOS。我问如何调整这一点,以便在