【发布时间】:2016-09-27 23:41:04
【问题描述】:
我的问题是我的用户输入的降雨总量没有正确加起来
我的假设是,当被问到多年时,它不会将第一个月添加为
def main():
#define accumulators
monthRain = 0
year = 0
monthTotal = 0
months = 0
total = 0
#get # of years
year = int(input("Enter the number of years to collect data for: "))
#define month total befor it is changed below with year + 1
monthTotal = year * 12
#define how many months per year
months = 12
#Find average rainfall per month
for year in range(year):
#accumulator for rain per month
total = 0
#get rainfall per month
print('\nNext you will enter 12 months of rainfall data for year', year + 1)
for month in range(months):
print("Enter the rainfall for month", month + 1, end=" ")
monthRain = float(input(': '))
#add monthly raingfall to accumulator
total += monthRain
average = total / monthTotal
#total months of data
print('\nYou have entered data for', monthTotal,'months')
#total rainfall
print('\nThe total rainfall for the collected months is:', total)
print('The average monthly rainfall for the collected months is:', format(average, '.2f' ))
main()
【问题讨论】: