大佬教程收集整理的这篇文章主要介绍了需要将文本文件中的每 12 个整数除以 12 以模拟一年中的几个月,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建一个程序,将文本文件中的每 12 个整数除以模拟获取一年的平均降雨量,举个例子,如果在单独的行中有 15 个整数,它将取第一个12 并将总数除以 12 以获得该年的平均值,然后它将最后三个分开并使其成为新的一年,我正在考虑制作一个将月份计数器重置为 0 的 else 语句,但我可以使用一些建议。
def main():
rain_per_month = open('RainperMonth.txt','r')
#this is to open the text file with the numbers
months = 0
#start the accumulator to keep track of the @R_318_10586@l amount of months
rainfall = 0
#this is to keep track of the @R_318_10586@l amount of rainfall
years = 0
for line in rain_per_month:
#this is a loop that will go line by line and begin to modify the variables
months += 1
#this is increasing the number of months according to the number of lines
#for example if there are 15 lines it will keep count of those 15 lines as seperate months
rainfall = int(linE) + rainfall
#this will convert the numbers on the line from being a symbol for 4 to really being the number 4 and
adding it to the @R_318_10586@l amount of rainfall
if months % 12 == 0:
avg_rain = rainfall / months
#Where I am going to seperate the different months and calculate the @R_318_10586@l for eac
print(avg_rain)
#where the caculations for the rain will be made
main()
只是稍微修改了您的逻辑。 步骤 ->
def main():
with open('RainperMonth.txt','r') as f:
content = f.read().splitlines()
for years,i in enumerate(range(0,len(content),12),start=1):
rain_per_month = content[i:i+12]
# print(rain_per_month)
rainfall = 0
for months,line in enumerate(rain_per_month,start=1):
rainfall = int(linE) + rainfall
avg_rain = rainfall / len(rain_per_month)
print(f'avg_rain = {avg_rain} for year = {years}')
main()
以上是大佬教程为你收集整理的需要将文本文件中的每 12 个整数除以 12 以模拟一年中的几个月全部内容,希望文章能够帮你解决需要将文本文件中的每 12 个整数除以 12 以模拟一年中的几个月所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。