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

为何Python个税计算器返回函数对象而非预期税款数值?

个税计算器代码问题修复

问题原因

  1. 错误赋值函数对象:在income_tax函数中,调用income_tax_single(income)或income_tax_married_jointly(income)后,未接收返回值,反而把tax_owed赋值为当前函数income_tax本身,导致返回的是函数对象而非计算出的税款。
  2. 未处理返回元组:两个计算函数返回的是(应缴税款, 税率档位)的元组,需要提取第一个元素作为最终结果。

修正后的代码

def income_tax(income, status = "single"):
     if status == 'single':
        tax_owed, _ = income_tax_single(income)  # 接收返回元组,忽略税率档位
        
     elif status == 'married filing jointly':
        tax_owed, _ = income_tax_married_jointly(income)
     else:
        print("status must be either 'single' or 'married filing jointly'. You entered 'status = ", status," '")
        return
        
     return tax_owed

def income_tax_married_jointly(income):
    
    income = round(income) ## round the income to nearest integer

    if income <= 19750:
        tax_bracket = 0.1
        income_tax = 0.1 * income
    elif (income >= 19751) and (income <= 80250):
        tax_bracket = 0.12
        income_tax = 1975 + 0.12 * (income - 19750)
    elif (income >= 80251) and (income <= 171050):
        tax_bracket = 0.22
        income_tax = 9235 + 0.22 * (income - 80250)
    elif (income >= 171051) and (income <= 326600):
        tax_bracket = 0.24
        income_tax = 29211 + 0.24 * (income - 171050)
    elif (income >= 326601) and (income <= 414700):
        tax_bracket = 0.32
        income_tax = 66543 + 0.32 * (income - 326601)
    elif (income >= 414701) and (income <= 622050):
        tax_bracket = 0.35
        income_tax = 94735 + 0.35 * (income - 414700)
    elif income >= 622051:
        tax_bracket = 0.37
        income_tax = 167307.50 + 0.37 * (income - 622050)
    
    return income_tax, tax_bracket

def income_tax_single(income, round_income = True):
    
    if round_income == True:
        income = round(income) # round to nearest integer
    else:
        income = income # do not round it
    
    if income <= 9875:
        tax_bracket = 0.1
        income_tax = 0.1 * income
    elif (income >= 9876
         ) and (income <= 40125):
        tax_bracket = 0.12
        income_tax = 987.50 + 0.12 * (income - 9875)
    elif (income >= 40126) and (income <= 85525):
        tax_bracket = 0.22
        income_tax = 4617.50 + 0.22 * (income - 40125)
    elif (income >= 85526) and (income <= 163300):
        tax_bracket = 0.24
        income_tax = 14605.50 + 0.24 * (income - 85525)
    elif (income >= 163301) and (income <= 207350):
        tax_bracket = 0.32
        income_tax = 33271.50 + 0.32 * (income - 163300)
    elif (income >= 207351) and (income <= 518400):
        tax_bracket = 0.35
        income_tax = 47367.50 + 0.35 * (income - 207350)
    elif income >= 518401:
        tax_bracket = 0.37
        income_tax = 156235 + 0.37 * (income - 518400)
    
    
    return income_tax, tax_bracket

验证结果

调用income_tax(10000, 'single')将返回1000.0,与预期结果一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 22:53:15