Python - 古代税收

时间:2013-10-01 15:56:56

标签: python for-loop

在这个问题中,它表示前10,000个税收不征税,接下来的20,000个税收10%,接下来的40,000个税率20%,前70,000个税收30%。我对python很新,但这是我到目前为止所做的。我不确定我哪里出错了。我认为这是因为我没有定义“税”变量,但我不确定。任何帮助将非常感激。谢谢!

**如果用户输入负数并且我不确定如何将其添加到for循环中,则代码也必须终止。

def income(drach):

    for drach in range(10000):
        tax = 0
    for drach in range(10000 , 30000):
        tax = ((drach - 10000)*0.1)
    for drach in range(30000 , 70000):
        tax = ((drach - 30000)*0.2) + 2000
    for drach in range(70000 , 10**999):
        tax = ((drach - 70000)*0.3) + 10000

    print tax

6 个答案:

答案 0 :(得分:5)

我认为这是正确的税收模式:

   def tax(income):
        tax=0;
        if income > 70000 : 
            tax += (income-70000)* 0.3 
            income = 70000
        if income > 30000 : 
            tax += (income-30000)* 0.2
            income = 30000
        if income > 10000 : 
            tax += (income-10000)* 0.1
        return tax;

答案 1 :(得分:2)

您很可能不想使用for in构造。在for x in iterable构造中,您循环iterablex分配给iterable下一个产生的元素,直到您到达终点(即StopIteration被提出。)

相反,您可能希望保留参数drach并将其应用于税收条件。

正如Sven Marnach所指出的那样:

  

范围(10000,30000)中的检查drach在Python 2中效率非常低。更好地使用10000< = drach< 30000

所以:

def income(drach):
    tax = 0
    if 10000 <= drach <= 30000:
        tax += ((drach - 10000)*0.1)
    if 30000 <= drach <= 70000:
        tax += ((drach - 30000)*0.2) + 2000
    if drach > 70000:
        tax += ((drach - 70000)*0.3) + 10000

    return tax

作为替代方案,您可以循环切片:

def income(drach):
    tax = 0
    percent = step = 0.1
    lower = 10000
    for upper in [30000, 70000]:
        if lower < drach <= upper:
            tax += (drach - lower) * percent
        lower = upper
        percentage += step
    if lower < drach:
        tax += (drach - lower) * percent
    return tax

答案 2 :(得分:2)

关键字for用于循环迭代。例如

for drach in range(10000):
    tax = 0

将范围0,1,2,...,9998,9999中的每个值分配给drach,并为每个值执行tax = 0。这几乎肯定不是你想要的 - 你可能想要if而不是for

您可以使用max()功能来避免使用if

tax = max(drach - 10000, 0) * 0.1 + 
      max(drach - 30000, 0) * 0.1 +
      max(drach - 70000, 0) * 0.1

答案 3 :(得分:0)

关于负数位 -

if drach < 0:
    break

当drach为负时将终止for循环。有关这方面的更多信息,请参阅更多有关Python's Flow Control tools.的信息。当您收到用户输入时,您应该这样做。不是在计算用户税时。

关于计算税金,您不希望在drachs范围内进行循环。这比你需要的计算更多。

您需要计算所谓的Progressive Tax

这是我的数据结构和用于计算累进税的相关函数。 编辑 - 我的功能犯了一些错误。现在应该是正确的。

tax_bracket = (
       #(from, to, percentage)
        (0,     10000, None),
        (10000, 30000, .10),
        (30000, 70000, .20),
        (70000, None,  .30),
        )


def calc_tax(drach):
    tax = 0

    for bracket in tax_bracket:
        # If there is no tax rate for this bracket, move on
        if not bracket[2]:
            continue

        # Check if we belong in this bracket
        if drach < bracket[0]:
            break

        # Last bracket
        if not bracket[1]:
            tax += ( drach - bracket[0] ) * bracket[2]
            break

        if drach >= bracket[0] and drach >= bracket[1]:
            tax += ( bracket[1] - bracket[0] ) * bracket[2]
        elif drach >= bracket[0] and drach <= bracket[1]:
            tax += ( drach - bracket[0] ) * bracket[2]
        else:
            print "Error"

    return tax



income = 50000

tax = calc_tax(income)
print tax

答案 4 :(得分:0)

以下是使用bisect的解决方案:

from bisect import bisect
def income(drach):
    tax_brackets = [10000, 30000, 70000]
    rates = [lambda i: 0.0,
             lambda i: (i - 10000) * 0.1,
             lambda i: (i - 30000) * 0.2 + 2000,
             lambda i: (i - 70000) * 0.3 + 10000]
    return rates[bisect(tax_brackets, drach)](drach) if drach > 0 else None

>>> income(10000)
0.0
>>> income(15000)
500.0
>>> income(34400)
2880.0
>>> income(-15000)
None

答案 5 :(得分:0)

这是一个稍微更通用的解决方案,可与任何提供的税级括号一起使用:

#!/usr/bin/python

'''Calculates income taxes.'''


def tax_on_bracket(income, lower, higher, rate):

    '''Calculates income taxes for a specified bracket.

    Setting the higher bound to 0 signifies no upper bound.'''

    if income <= lower:
        return 0
    elif income >= higher and higher != 0:
        return (higher - lower) * rate
    else:
        return (income - lower) * rate


def tax(income, brackets):

    '''Calculates income taxes based on provided income and tax brackets.'''

    total_tax = 0
    for (lower, higher, rate) in brackets:
        total_tax += tax_on_bracket(income, lower, higher, rate)

    return total_tax


def main():

    '''Main function.'''

    brackets = [(0, 10000, 0),
                (10000, 30000, 0.1),
                (30000, 70000, 0.2),
                (70000, 0, 0.3)]

    print('Tax for 9000 drach: {0}'.format(tax(9000, brackets)))
    print('Tax for 18000 drach: {0}'.format(tax(18000, brackets)))
    print('Tax for 27000 drach: {0}'.format(tax(27000, brackets)))
    print('Tax for 55000 drach: {0}'.format(tax(55000, brackets)))
    print('Tax for 90000 drach: {0}'.format(tax(90000, brackets)))


if __name__ == "__main__":
    main()