6

在这个问题中,它说前 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
4

6 回答 6

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;
于 2013-10-01T16:15:59.133 回答
2

您很可能不想使用for in构造。在for x in iterable构造中,您循环iterable分配给由产生x下一个元素,直到到达末尾(即引发StopIteration )。iterable

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

正如Sven Marnach指出的那样:

在 Python 2 中检查范围内的 drach(10000 , 30000) 效率非常低。最好使用 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
于 2013-10-01T16:04:31.653 回答
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
于 2013-10-01T16:09:57.900 回答
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
于 2013-10-01T17:00:23.737 回答
0

关于负数位 -

if drach < 0:
    break

当 drach 为负数时将终止 for 循环。有关这方面的更多信息,请参阅有关Python 的流控制工具的更多信息。当您获得用户输入时,您应该这样做。不是在计算用户税时。

关于计算税收,您不会希望在drachs. 这比您需要的计算量要多得多。

您需要计算所谓的累进税

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

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
于 2013-10-01T16:02:09.490 回答
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()
于 2013-10-01T17:17:33.953 回答