【问题标题】:How could I use user_int_count to add years to the print function. Like if input the year 2000 and int count 5 it would print years 2000-2005我如何使用 user_int_count 将年份添加到打印功能。就像如果输入 2000 年和 int count 5 它会打印 2000-2005 年
【发布时间】:2015-10-23 15:56:56
【问题描述】:
def open_file():
    data=open("data_full.txt")
    return data


def process_file(data):
    out_file = input("Enter a name for the output file: ")
    output_file= open(out_file, "w")
    user_year = int(input("Enter a year: "))
    user_int_count= int(input("Enter a integer count: "))


    cnt = 0
    data.readline()
    for line in data:
        cnt+= 1
        field = line.strip().split()

        line_list = [int(n) for n in field]

        total = sum(line_list[1:])
        year = line_list[0]
        avg = int(round((total/12),0))

        if user_year == year:
            output_file.write("{:<d}  {:<d}".format(year, avg))
            print()
            print( "{:<d}  {:<d}".format(year, avg))

    output_file.close()

def main():
    while True:
        try:
            f=open_file()
            break
        except FileNotFoundError:
            print("Invalid file name. Try Again.")
    process_file(f)
    f.close()
main()        

【问题讨论】:

  • 我认为for i to range(user_int_count): print(User_year+i) 是您要找的?
  • 您的问题属于邮件正文,而不是标题。

标签: python for-loop int


【解决方案1】:

这应该打印从输入的年份到递增值的范围,假设这是您的要求。如果不是,请改写您的问题,因为它有点难以解释。

for x in range(user_year, user_year + count):
    print x

【讨论】:

    【解决方案2】:

    我是根据对问题的更字面解释来回答这个问题的。

    def year_range(year_date, number_of_years): #assuming these are of type int.
        output = '{}-{}'.format(year_date, year_date+number_of_years)
        print(output)
    

    所以 year_range(2000, 5)

    给你“2000-2005”

    【讨论】:

      猜你喜欢
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多