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

如何调整Python代码实现最低税额的目标输出格式?

最低税额计算输出修正方案

问题概述

我在计算并输出最低税额的项目中遇到格式和逻辑问题,当前代码输出不符合预期,需要调整代码实现目标输出样式。

当前输出

Income: 6100  
Lowest tax: 1540.0  
1540.0  
Norway  
 
Income: 9000  
Lowest tax: 2340.0  
2340.0  
Canada  

目标输出

Income: 1000000  
Lowest tax: 150000.0  
USA   
Income: 6000  
Lowest tax: 1500.0  
Denmark Norway USA   
Income: -1  

现有代码

continue_input = True
income = 0


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

    
    if income < 0 :
        continue_input = not True

    # Canada   
    canada_tax = 0.26
    canada = canada_tax * income

    # Norway
    if income > 3000:
        norway_tax1 = 0.1 * 3000
        tax_left = income - 3000
        norway_tax2 = 0.4 * tax_left
        norway_tax = norway_tax1 + norway_tax2
    elif income <= 3000:
        norway_tax = 0.1 * income

    # Denmark
    denmark_tax = 0
    percent = 0
    for _ in range(int(income/1000)):
      denmark_tax += percent * 1000
      percent += 0.1
    denmark_tax += percent * (income%1000)
      
    # USA
    if income <= 1500:
        USA_tax = 0.12 * income
    elif income > 1500 and income <= 6000:
        USA_tax = 0.25 * income
    elif income > 6000 and income <= 10000:
        USA_tax = 0.38 * income
    elif income > 10000:
        USA_tax = 0.15 * income
        

    
    min_tax = min(canada, norway_tax, USA_tax, denmark_tax)
    print(f'Lowest tax: {min_tax}')
    print(min_tax)

    if min_tax == canada :
        print('Canada')

    if min_tax == denmark_tax :
        print('Denmark')

    if min_tax == norway_tax :
        print('Norway')

    if min_tax == USA_tax :
        print('USA')
    print()

修改步骤

  • 移除重复的税额输出:删掉代码中第二个打印最低税额的语句print(min_tax)
  • 合并多国家输出:当多个国家税额同为最低时,将名称拼接成一行打印,而非逐个单独输出
  • 优化终止逻辑:输入负数时直接终止循环,跳过后续税额计算与打印流程
  • 统一格式:确保国家名称在同一行输出,避免多余空行

修改后的代码

continue_input = True

while continue_input:
    income = int(input('Income: '))
    
    if income < 0:
        continue_input = False
        continue
    
    # 计算各国税额
    canada = 0.26 * income
    
    norway_tax = 0.1 * 3000 + 0.4 * (income - 3000) if income > 3000 else 0.1 * income
    
    denmark_tax = 0
    percent = 0
    for _ in range(int(income // 1000)):
        denmark_tax += percent * 1000
        percent += 0.1
    denmark_tax += percent * (income % 1000)
    
    if income <= 1500:
        usa_tax = 0.12 * income
    elif 1500 < income <= 6000:
        usa_tax = 0.25 * income
    elif 6000 < income <= 10000:
        usa_tax = 0.38 * income
    else:
        usa_tax = 0.15 * income
    
    # 映射税额与对应国家
    tax_countries = [
        (canada, 'Canada'),
        (norway_tax, 'Norway'),
        (denmark_tax, 'Denmark'),
        (usa_tax, 'USA')
    ]
    
    min_tax = min(tax[0] for tax in tax_countries)
    # 筛选所有最低税额对应的国家
    min_countries = [country for tax, country in tax_countries if tax == min_tax]
    
    # 按目标格式输出
    print(f'Lowest tax: {min_tax}')
    print(' '.join(min_countries))
    print()

效果验证

输入1000000时,USA税额为150000.0(最低值),输出对应内容;输入6000时,Denmark、Norway、USA税额均为1500.0,会在同一行输出三个国家名称;输入-1时直接终止程序,无多余输出。

内容的提问来源于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.02 19:01:28