Slow Poke Slow Poke - 1 year ago 92
Python Question

Cant get inner loops to add up properly

My issue is with my user input totals for rainfall not adding up properly

my assumption is that it is not adding the 1st month in when asked on multiple years as

problem explained

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' ))


Answer Source

Take total = 0 out of the for loop. Every time you enter the loop, it sets total to zero. Take it out and put it before the for loop. I think that will fix it.

Let me know...