2017-04-20 83 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) 
相關問題