Python 如何使用user_int_count向打印功能添加年份。例如,如果输入2000年和int count 5,它将打印2000-2005年 def open_file(): 数据=打开(“data_full.txt”) 返回数据 def过程_文件(数据): 输出文件=输入(“输入输出文件的名称:”) 输出文件=打开(输出文件,“w”) 用户_year=int(输入(“输入年份:”) user\u int\u count=int(输入(“输入整数计数”)) cnt=0 data.readline() 对于行输入数据: cnt+=1 field=line.strip().split() 行列表=[int(n)表示字段中的n] 总计=总和(行列表[1:]) 年份=行列表[0] 平均值=整数(四舍五入((总计/12),0)) 如果用户年==年: 输出文件.write(“{:
这应该打印一个从输入的年份到递增值的范围,假设这是您要问的。如果不是,请重新表述您的问题,因为它有点难以解释Python 如何使用user_int_count向打印功能添加年份。例如,如果输入2000年和int count 5,它将打印2000-2005年 def open_file(): 数据=打开(“data_full.txt”) 返回数据 def过程_文件(数据): 输出文件=输入(“输入输出文件的名称:”) 输出文件=打开(输出文件,“w”) 用户_year=int(输入(“输入年份:”) user\u int\u count=int(输入(“输入整数计数”)) cnt=0 data.readline() 对于行输入数据: cnt+=1 field=line.strip().split() 行列表=[int(n)表示字段中的n] 总计=总和(行列表[1:]) 年份=行列表[0] 平均值=整数(四舍五入((总计/12),0)) 如果用户年==年: 输出文件.write(“{:,python,for-loop,int,Python,For Loop,Int,这应该打印一个从输入的年份到递增值的范围,假设这是您要问的。如果不是,请重新表述您的问题,因为它有点难以解释 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_
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 x in range(user_year, user_year + count):
print x
so年份范围(2000年,第5年)
为您提供“2000-2005”我认为
的i到范围(用户数):print(用户数+i)
是您想要的吗?您的问题属于邮件正文,而不是标题。
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)