income tax calculation python

 ̄綄美尐妖づ 提交于 2019-12-14 01:16:26

问题


How do I go about making a for-loop with a range 70000 and above? I'm doing a for-loop for an income tax and when income is above 70000 there is a tax of 30%. Would i do something like for income in range(income-70000)?

Well, at first i developed a code that didn't use a loop and it worked just fine, but then i was notified that i needed to incorporate a loop in my code. This is what i have, but it just doesn't make sense for me to use a for loop. Can someone help me?

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)

Okay, so I have now tried with a while loop, but it doesn't return a value. Tell me what you think. I need to calculate the income tax based on an income. first 10000 dollars there is no tax. next 20000 there is 10%. Next 40000 there is 20%. and above 70000 is 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

回答1:


Q: How do I go about making a for-loop with a range 70000 and above?

A: Use the itertools.count() method:

import itertools

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

Q: I need to calculate the income tax based on an income. first 10000 dollars there is no tax. next 20000 there is 10%. Next 40000 there is 20%. and above 70000 is 30%.

A: The bisect module is great for doing lookups in ranges:

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



回答2:


If you really must loop, one approach would be to add up the tax for each unit of income separately:

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



回答3:


Both your function definitely do not compute needed value. You need something like:

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



回答4:


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



回答5:


I dont know any python, but your problems is not the language either. You need to read about conditionals. You dont need all that FOR, just 1 and do the IFS according to your rules.



来源:https://stackoverflow.com/questions/20130478/income-tax-calculation-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!