我有一个func calculate_tax应该将 name:salary 对作为参数,计算税后然后返回 name: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}
它应该将Patrick的税收归还为2490
答案 0 :(得分:0)
calculate_tax中的错误方法,税收为2490美元,通过3级税基。
因此,您应该计算收入 - 税基,直到其他人无需纳税。
您可以使用while
循环来执行此操作。
+++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++
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)
Patrick 2490.0 肯0 小熊15352.5
答案 1 :(得分: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)