【问题标题】:Can't figure out how to print the following outputs无法弄清楚如何打印以下输出
【发布时间】:2023-01-29 21:31:24
【问题描述】:

我正在从我的项目中打印出最低税,但我被这个问题困住了。我怎样才能改变这个输出:

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()

【问题讨论】:

  • 您可以通过在获得的和期望的输出中输入相同的收入来更精确吗..

标签: python if-statement while-loop printing


【解决方案1】:
  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() 将阻止它添加换行符。它会添加一个空格。

【讨论】:

  • 输入 <0 后代码应该停止
  • @BobbyJackson 是的,所以我建议用“break”替换“continue_input = not True”。 Break 会立即跳出 while 循环。
  • okey 会这样做,但输出仍然相同
  • @BobbyJackson 好吧,我更新了我的答案,也把国家名都放在了同一行
  • 如果它对你有用,你能“接受”这个答案吗?
猜你喜欢
  • 2018-10-23
  • 1970-01-01
  • 2014-12-18
  • 2013-10-07
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多