You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用if语句或for loop实现丹麦阶梯式个税计算?

实现丹麦阶梯个税计算的Python方案

需求说明

个税计算规则:

  • 首个1000欧元免税(税率0%)
  • 第二个1000欧元税率10%
  • 第三个1000欧元税率20%
  • 每增加1000欧元区间,税率提升10个百分点
  • 示例:输入6100欧元时,计算过程为:
    0.0 * 1000 + 0.1 * 1000 + 0.2 * 1000 + 0.3 * 1000 + 0.4 * 1000 + 0.5 * 1000 + 0.6 * 100 = 1560.0

我尝试了以下代码但无法完成完整计算,希望用if语句或for/while循环实现:

income = int(input('Income: '))

percent_counter = 0
income_counter = 0

if income > 1000:
    no_tax = 0.0 * 1000
    percent_counter += 0.1
    income_counter += 1000 
else:
    no_tax = 0.0 * income

解决方案

方法1:用While循环实现(推荐)

这种方式能处理任意金额的收入,逻辑清晰且扩展性强:

income = int(input('Income: '))

total_tax = 0.0
bracket = 1000  # 每个计税区间的额度
current_rate = 0.0  # 首个区间税率0%
remaining = income

while remaining > 0:
    # 计算当前区间需计税的金额,不超过区间上限
    taxable = min(remaining, bracket)
    total_tax += current_rate * taxable
    # 进入下一个区间,税率提升10%
    current_rate += 0.1
    remaining -= taxable

print(f"应缴税额:{total_tax}")

测试输入6100,输出1560.0,完全符合预期。

方法2:用If语句实现(适合区间较少的场景)

如果收入范围有限,可以通过嵌套/链式if处理,但扩展性差,收入过高时需要不断添加分支:

income = int(input('Income: '))
total_tax = 0.0

if income <= 1000:
    total_tax = 0.0
else:
    remaining = income - 1000
    # 第二个区间(1001-2000):10%税率
    if remaining <= 1000:
        total_tax += remaining * 0.1
    else:
        total_tax += 1000 * 0.1
        remaining -= 1000
        # 第三个区间(2001-3000):20%税率
        if remaining <= 1000:
            total_tax += remaining * 0.2
        else:
            total_tax += 1000 * 0.2
            remaining -= 1000
            # 第四个区间(3001-4000):30%税率
            if remaining <= 1000:
                total_tax += remaining * 0.3
            else:
                total_tax += 1000 * 0.3
                remaining -= 1000
                # 剩余部分按对应税率计算,可继续延伸逻辑
                total_tax += remaining * 0.4 if remaining <=1000 else 1000*0.4 + (remaining-1000)*0.5

print(f"应缴税额:{total_tax}")

内容的提问来源于stack exchange,提问作者Bobby Jackson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 11:20:37