我可以使用python根据字典的输入收入来计算税吗?

2020-03-26 python

这些是税率:

Net chargeable Income($)    Rate    Tax($)    
On the First 50,000         2%  
On the Next 50,000          6%  
On the Next 50,000          10% 
On the Next 50,000          14% 
Remainder                   17% 

这些是客户及其收入:

Client  Total Income
Amy      45,000
Bob     80,000
Jane    135,000
Steve   187,500
Hovy    250,000
Will    313,000

我想编写代码以打印每个客户的2019年税款,如下所示:

{'Amy': 900.0, 'Bob': 2800.0, 'Jane': 7500.0, 'Steve': 14250.0, 'Hovy': 24500.0, 'Will': 35210.0}

我的代码是这样的:

income = {'Amy':45000, 'Bob':80000, 'Jane':135000, 'Steve':187500, 'Hovy':250000, 'Will':313000}
tax = {'Amy':0, 'Bob':0, 'Jane':0, 'Steve':0, 'Hovy':0, 'Will':0}
def question_11(income=income):
    client_list=list(income.values())
    for i in range(len(client_list)):
        if client_list[i]<50000:
            tax[i]=0
        elif 100000>=client_list[i]>=50000:
            tax[i]=1000+(client_list[i]-50000)*0.06
        elif 150000>=client_list[i]>100000:
            tax[i]=4000+(client_list[i]-100000)*0.1
        elif 200000>=client_list[i]>150000:
            tax[i]=9000+(client_list[i]-150000)*0.14
        elif client_list[i]>200000:
            tax[i]=16000+(client_list[i]-200000)*0.17
    for name in tax.keys():
        (tax.keys[i])=str(name)

    for tax_num  in tax.values():
        (tax[i])=int(tax_num)

    dict1={name:tax_num}
    print(dic1)

该代码在执行错误时存在一些问题,但是我确信计算税额的内在逻辑是正确的。您能给我一些指示吗?谢谢!

Answers

IIUC:试试这个:

def question_11(income_dict=income):
    tax_dict = dict()
    for person, income in income_dict.items():
        if income <= 50000:
            tax = 0.02 * income
        elif income >= 50000 and income < 100000:
            tax = 0.02 * 50000 + 0.06 * (income - 50000)
        elif income >= 100000 and income < 150000:
            tax = 0.02 * 50000 + 0.06 * 50000 + 0.1 * (income - 100000)
        elif income >= 150000 and income < 200000:
            tax = 0.02 * 50000 + 0.06 * 50000 + 0.1 * 50000 + 0.14 * (income - 150000)
        else:
            tax = 0.02 * 50000 + 0.06 * 50000 + 0.1 * 50000 + 0.14 * 50000 + 0.17 * (income - 200000)

        tax_dict[person] = tax

    return tax_dict

print(question_11())

输出:

{'Amy': 900.0, 'Bob': 2800.0, 'Jane': 7500.0, 'Steve': 14250.0, 'Hovy': 24500.0, 'Will': 35210.0}

Related