2017-04-20 94 views
0

我有一个FUNC calculate_tax应该采取名字:薪酬对作为一个参数,计算则纳税申报名字的字典:total_tax对但是,似乎并没有正确计算税收。我可能做错了什么?下面是代码:传递一个字典功能,操纵值并返回一个字典

def calculate_tax(**data): 
    for key in data: 
     if data[key] > 0 and data[key] <= 1000: 
      data[key] = 0 
     elif data[key] > 1000 and data[key] <= 10000: 
      data[key] += (data[key]-1000)*0.1 
     elif data[key] > 10000 and data[key] <= 20200: 
      data[key] += (data[key]-10000)*0.15 
     elif data[key] > 20200 and data[key] <= 30750: 
      data[key] += (data[key]-20200)*0.2 
     elif data[key] > 30750 and data[key] <= 50000: 
      data[key] += (data[key]-30750)*0.25 
     elif data[key] > 50000: 
      data[key] += (data[key]-50000)*0.3 
    return data 

的税率是:

Yearly Income: 0 - 1000 
Tax Rate: 0% 

Yearly Income: 1,001 - 10,000 
Tax Rate: 10% 

Yearly Income: 10,001 - 20,200 
Tax Rate: 15% 

Yearly Income: 20,201 - 30,750 
Tax Rate: 20% 

Yearly Income: 30,751 - 50,000 
Tax Rate: 25% 

Yearly Income: Over 50,000 
Tax Rate: 30% 

例如,假设当:

{'Ken':500,'Patrick':20500,'Winnie':70000} 

它应该返回帕特里克税2490

+0

我得到的结果是:{'Patrick':20560.0,'Ken':0,'Winnie':76000.0}这似乎是错误的 – Hillux

+2

'帕特里克':20500 - 它应该返回帕特里克税为2490' - 为什么?这将是'20500 +(20500 - 20200)* 0.2 = 2560' –

+0

@Hillux可以提供您想要使用数学表达式计算税款的方式吗?您错过了操作顺序优先顺序。 – direprobs

回答

0

错误的方法在calculate_tax中,作为税款2490美元,它是通过了3级税基

因此,您应该计算收入税基,直到其余无需交税。

您可以使用while循环来做到这一点。

BTW,我这么做是为了你=]

+++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++

def calculate_tax(data_dict): 
    for person in data_dict: 
     income_need_to_pay_tax = data_dict[person] 
     tax = 0 
     while income_need_to_pay_tax > 1000: 
      if income_need_to_pay_tax > 50000: 
       income_in_tax_base = income_need_to_pay_tax - 50000 
       income_need_to_pay_tax -= income_in_tax_base 
       tax += income_in_tax_base*0.3 
      if income_need_to_pay_tax > 30750: 
       income_in_tax_base = income_need_to_pay_tax - 30750 
       income_need_to_pay_tax -= income_in_tax_base 
       tax += income_in_tax_base*0.25 
      if income_need_to_pay_tax > 20200: 
       income_in_tax_base = income_need_to_pay_tax - 20200 
       income_need_to_pay_tax -= income_in_tax_base 
       tax += income_in_tax_base*0.2 
      if income_need_to_pay_tax > 10000: 
       income_in_tax_base = income_need_to_pay_tax - 10000 
       income_need_to_pay_tax -= income_in_tax_base 
       tax += income_in_tax_base*0.15 
      if income_need_to_pay_tax > 1000: 
       income_in_tax_base = income_need_to_pay_tax - 1000 
       income_need_to_pay_tax -= income_in_tax_base 
       tax += income_in_tax_base * 0.1 
     print(person, tax) 

帕特里克2490.0 肯0 小熊15352.5

+0

这真的很不错,但我需要返回字典的函数 – Hillux

+0

只需在while循环后面加上'data_dict [person] = tax',这对你来说应该不难。 –

0

您正在寻找这样的事情?:

def calculate_tax(data_dict): 
    data_output = [] 

    for person in data_dict: 
     income_need_to_pay_tax = data_dict[person] 
     tax = 0 
     while income_need_to_pay_tax > 1000: 
      if income_need_to_pay_tax > 50000: 
       income_in_tax_base = income_need_to_pay_tax - 50000 
       income_need_to_pay_tax -= income_in_tax_base 
       tax += income_in_tax_base*0.3 
      if income_need_to_pay_tax > 30750: 
       income_in_tax_base = income_need_to_pay_tax - 30750 
       income_need_to_pay_tax -= income_in_tax_base 
       tax += income_in_tax_base*0.25 
      if income_need_to_pay_tax > 20200: 
       income_in_tax_base = income_need_to_pay_tax - 20200 
       income_need_to_pay_tax -= income_in_tax_base 
       tax += income_in_tax_base*0.2 
      if income_need_to_pay_tax > 10000: 
       income_in_tax_base = income_need_to_pay_tax - 10000 
       income_need_to_pay_tax -= income_in_tax_base 
       tax += income_in_tax_base*0.15 
      if income_need_to_pay_tax > 1000: 
       income_in_tax_base = income_need_to_pay_tax - 1000 
       income_need_to_pay_tax -= income_in_tax_base 
       tax += income_in_tax_base * 0.1 
      data_output.append((person, tax))   
    return dict(data_output)