def calculate_tax(income, tax_ranges):
tax = 0
for lower, upper, rate in tax_ranges:
if income <= lower:
continue
elif income > lower and income <= upper:
taxable_income = income - lower
tax += taxable_income * float(rate / 100.0)
else:
taxable_income = upper - lower
tax += taxable_income * float(rate / 100.0)
return tax
# 第一个国家的工资税收
country_1_tax_ranges = [
(70000, float('inf'), 39),
(60000, 70000, 36),
(40000, 60000, 33),
(38000, 40000, 27),
(14000, 38000, 21),
(9500, 14000, 16.75),
(0, 9500, 13.75)
]
# 第二个国家的工资税收
country_2_tax_ranges = [
(960000, float('inf'), 45),
(660000, 960000, 35),
(420000, 660000, 30),
(300000, 420000, 25),
(144000, 300000, 20),
(36000, 144000, 10),
(0, 36000, 3)
]
salary = 150000
# 在第一个国家计算逐段扣除不同段的个税
country_1_tax = calculate_tax(salary, country_1_tax_ranges)
# 在第二个国家计算逐段扣除不同段的个税
country_2_tax = calculate_tax(salary, country_2_tax_ranges)
print("在第一个国家工资为", salary, "时需要缴纳的所得税为:", country_1_tax)
print("在第二个国家工资为", salary, "时需要缴纳的所得税为:", country_2_tax)
continue,可避免不必要的计算和判断,提高代码的执行效率。
由于循环的顺序已经按照收入范围从小到大排列,因此无需继续执行后续循环,
因为后续循环的起征点都会小于当前收入。因此,可以使用 continue 跳过这些不符合条件的循环,
直到找到适用的税率段进行个税计算。
哎~ ,写了一个上午 ,转行即落伍啊。
版权所有 , 转载需注明来源。
版权声明:本文为haimian520原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。