【问题标题】:How to start table with correct output in python如何在python中以正确的输出开始表
【发布时间】:2021-09-13 04:42:47
【问题描述】:

我目前是 Python 编码的新手,并且正在为一小段我似乎无法理解的代码而苦苦挣扎。我的代码本质上是接受起薪并输出在用户输入多少年的过程中的工资增长,并从输出中制作一个表格。我的代码中的所有内容都是正确的,除了一个问题。我需要以用户输入的起薪开始表格,而不是以您在第 2 年的收入开始表格。

percent = float(input("Enter the annual percent increase: "))
years = int(input("Enter the number of years: "))
percent = percent / 100
print("%-7s%10s" % ("Year","Salary"))
for i in range(1,years):
    starting += (starting * percent)
    print("%2d%17.2f" % (i, starting))

【问题讨论】:

    标签: python math logic


    【解决方案1】:

    尝试交换打印顺序和添加:

    for i in range(1,years):
        print("%2d%17.2f" % (i, starting))
        starting += (starting * percent)
    

    【讨论】:

      【解决方案2】:

      试试这个

       starting = 1000  # sample starting
      
      percent = float(input("Enter the annual percent increase: "))
      years = int(input("Enter the number of years: "))
      percent /= 100
      print("%-7s%10s" % ("Year", "Salary"))
      for i in range(years):
      
          print("%2d%17.2f" % (i, starting))
          starting += (starting * percent)
      

      会给

      Year       Salary
       0          1000.00
       1          1030.00
       2          1060.90
      

      【讨论】:

        猜你喜欢
        • 2020-02-22
        • 1970-01-01
        • 1970-01-01
        • 2018-10-22
        • 1970-01-01
        • 2013-01-16
        • 1970-01-01
        • 2011-04-27
        • 2020-08-18
        相关资源
        最近更新 更多