简体   繁体   中英

How to run the for-loop then make the calculations at the end

I am working on a problem where I have to calculate the average amount of rainfall based on the user-input number of years and the user-input inches of each month .

What I want to do is:

Calculate the grand total of every year and its average at the end, rather than doing this within each iteration of the for-loop and I'm drawing a blank on how to do this.

Here is my code:

MONTHS = 12
total = 0.0

while True:
  try:
    years_of_rain = int(input('How many years of rainfall would you like to calculate?: '))

    if years_of_rain <= 0 :
        print('Invalid data. You must enter 1 or more years. Try again.')
        continue

    for y in range(years_of_rain) :
        jan = float(input('How many inches of rain fell in January of year ' + str(y + 1) + '?: '))
        feb = float(input('How many inches of rain fell in February of year ' + str(y + 1) + '?: '))
        mar = float(input('How many inches of rain fell in March of year ' + str(y + 1) + '?: '))
        apr = float(input('How many inches of rain fell in April of year ' + str(y + 1) + '?: '))
        may = float(input('How many inches of rain fell in May of year ' + str(y + 1) + '?: '))
        jun = float(input('How many inches of rain fell in June of year ' + str(y + 1) + '?: '))
        jul = float(input('How many inches of rain fell in July of year ' + str(y + 1) + '?: '))
        aug = float(input('How many inches of rain fell in August of year ' + str(y + 1) + '?: '))
        sep = float(input('How many inches of rain fell in September of year ' + str(y + 1) + '?: '))
        oct = float(input('How many inches of rain fell in October of year ' + str(y + 1) + '?: '))
        nov = float(input('How many inches of rain fell in November of year ' + str(y + 1) + '?: '))
        dec = float(input('How many inches of rain fell in December of year ' + str(y + 1) + '?: '))

        rain_average_calc = (jan + feb + mar + apr + may + jun + jul + aug + sep + oct + nov + dec) / MONTHS
        total += jan + feb + mar + apr + may + jun + jul + aug + sep + oct + nov + dec

        num_months = MONTHS * years_of_rain

        average = total / num_months

        print('The total amount of rain was ' + format(total , ',.2f') + ' inches' )
        print('the average amount of rain was ' + format(rain_average_calc , ',.1f') + ' inches per month.' )
        print(average)
        print(num_months)

    break
except:
    print('invalid. try again')

Declare total before entering the for loop so it isn't reset every iteration, add the total amount of rain for year y to total every iteration, then calculate and print your results after exiting the loop. Something like this:

# Outside of for loop
total = 0

for y in range(years_of_rain):
     # Inside of for loop

     # Code for getting input for current year goes here

     total += jan + feb + mar + apr + may + jun + jul + aug + sep + oct + nov + dec

# Outside of for loop, after it has finished

num_months = MONTHS * years_of_rain
average = total / num_months

print('The total amount of rain was ' + format(total , ',.2f') + ' inches' )
print('the average amount of rain was ' + format(average , ',.1f') + ' inches per month.' )

Try using data structures, in this case I would suggest creating a list to store all the values and instantiate it before the for loop. Then it will still exist after the for loop and you can do your calculations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM