外部嵌套循环的累加器 Python
Accumulator for outer nested loop Python
我正在通过书本自学 python 并解决问题。在一个问题中,用户输入几年中一年中每个月的降雨量。我需要找到每年的平均降雨量(总和(月雨)/numb_months,以及那段时间的平均降雨量,例如两年。在下面的代码中,我可以获得平均值每年(我只用了 3 个月而不是 12 个月以避免现在繁琐的输入),但我不知道我需要在哪里放置一个蓄能器来计算那段时间的总雨量然后取平均值。感谢你的帮助。
number_of_months = 3
years_in_period = int(input("Please enter the number of years in the period. \n"))
for year in range(years_in_period):
yearly_rain = 0
print('Year', year+1)
print('−−−−−−−−−−−−−−−−−')
for month in range(number_of_months):
print('Month', month+1, end='')
monthly_rain = float(input("Please enter rainfall for this month: \n"))
yearly_rain += monthly_rain
average_yearly_rainfall = yearly_rain / number_of_months
print("Average yearly rainfall of year ", year+1, " is ", average_yearly_rainfall)
print("Year total rain is", yearly_rain)
print()
如果我明白你想要什么(计算期间降雨量的绝对平均值),这应该可以解决问题:
number_of_months = 3
years_in_period = int(input("Please enter the number of years in the period. \n"))
total_rain = 0
for year in range(years_in_period):
yearly_rain = 0
print('Year', year+1)
print('−−−−−−−−−−−−−−−−−')
for month in range(number_of_months):
print('Month', month+1, end='')
monthly_rain = float(input("Please enter rainfall for this month: \n"))
yearly_rain += monthly_rain
total_rain += monthly_rain
average_yearly_rainfall = yearly_rain / number_of_months
print("Average yearly rainfall of year ", year+1, " is ", average_yearly_rainfall)
print("Year total rain is", yearly_rain)
print()
total_months = years_in_period * number_of_months
print("Absolute average of rain/month was", total_rain/total_months)
print("Absolute average of rain/year was", total_rain/years_in_period)
我正在通过书本自学 python 并解决问题。在一个问题中,用户输入几年中一年中每个月的降雨量。我需要找到每年的平均降雨量(总和(月雨)/numb_months,以及那段时间的平均降雨量,例如两年。在下面的代码中,我可以获得平均值每年(我只用了 3 个月而不是 12 个月以避免现在繁琐的输入),但我不知道我需要在哪里放置一个蓄能器来计算那段时间的总雨量然后取平均值。感谢你的帮助。
number_of_months = 3
years_in_period = int(input("Please enter the number of years in the period. \n"))
for year in range(years_in_period):
yearly_rain = 0
print('Year', year+1)
print('−−−−−−−−−−−−−−−−−')
for month in range(number_of_months):
print('Month', month+1, end='')
monthly_rain = float(input("Please enter rainfall for this month: \n"))
yearly_rain += monthly_rain
average_yearly_rainfall = yearly_rain / number_of_months
print("Average yearly rainfall of year ", year+1, " is ", average_yearly_rainfall)
print("Year total rain is", yearly_rain)
print()
如果我明白你想要什么(计算期间降雨量的绝对平均值),这应该可以解决问题:
number_of_months = 3
years_in_period = int(input("Please enter the number of years in the period. \n"))
total_rain = 0
for year in range(years_in_period):
yearly_rain = 0
print('Year', year+1)
print('−−−−−−−−−−−−−−−−−')
for month in range(number_of_months):
print('Month', month+1, end='')
monthly_rain = float(input("Please enter rainfall for this month: \n"))
yearly_rain += monthly_rain
total_rain += monthly_rain
average_yearly_rainfall = yearly_rain / number_of_months
print("Average yearly rainfall of year ", year+1, " is ", average_yearly_rainfall)
print("Year total rain is", yearly_rain)
print()
total_months = years_in_period * number_of_months
print("Absolute average of rain/month was", total_rain/total_months)
print("Absolute average of rain/year was", total_rain/years_in_period)