繁体   English   中英

无法弄清楚如何打印以下输出

[英]Can't figure out how to print the following outputs

我正在从我的项目中打印出最低税,但我被这个问题困住了。 如何更改此 output:

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()
  1. 如果收入 < 0,您想立即跳出 while 循环,因此:

    如果收入 < 0:中断

  2. 删除行print(min_tax) ,它会额外打印 min_tax 。

  3. 要将国家名称全部放在同一行,请执行以下操作:

    if min_tax == canada :
        print('Canada', end=" ")

    if min_tax == denmark_tax :
        print('Denmark', end=" ")

    if min_tax == norway_tax :
        print('Norway', end=" ")

    if min_tax == USA_tax :
        print('USA', end=" ")

    print()

end=" "添加到print()将阻止它添加换行符。 它将添加一个空格。

①如果本文未解决您的问题,请点击查看与本文相关的问题
②如果本文未解决您的问题,请向程序员专用AI小助手提问
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM