资讯动态

Ex: Tax Calculator 税务计算器

发布时间:2026/10/14 10:25:21 来源:尧图企业网站定制
问题描述分级税率定义如下希望定义一个税务计算器分层计算应缴纳的税费。brackets [ (0, 0.10), (10000, 0.20), (30000, 0.30) ]def calculate_tax(income, brackets): # Your code here tax 0 for k in brackets[::-1]: threshold k[0] tax_r k[1] if income threshold: tax (income - threshold)*tax_r income income - (income - threshold) return tax# Method 2: def calculate_tax(income, brackets): # Your code here tax 0 brackets.reverse() for k in brackets: threshold k[0] tax_r k[1] if income threshold: tax (income - threshold)*tax_r income income - (income - threshold) return tax# Test case calculate_tax(50000, brackets)Output:11000.0Noteslst.reverse()会直接修改原列表。reverse()返回的是None而不是反转后的列表不能写成lst lst.reverse()Error此时lst会变成None。使用切片[::-1]可以生成一个新列表lst [1, 2, 3, 4, 5] lst_reverse lst[::-1] print(lst_reverse)本税务计算器采用累进税率制。它不是将总收入直接乘以最高税率而是针对不同收入区间分别计算税额。完整可运行代码brackets [ (0, 0.10), (10000, 0.20), (30000, 0.30) ] def calculate_tax(income, brackets): # Your code here tax 0 for k in brackets[::-1]: threshold k[0] tax_r k[1] if income threshold: tax (income - threshold)*tax_r income income - (income - threshold) return tax # Test case print(calculate_tax(50000, brackets))

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑