Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rainfall Averages This python program will take user input over a number of year

ID: 3606430 • Letter: R

Question

Rainfall Averages

This python program will take user input over a number of years and compute the total months, total rainfall, and average rainfall.

Requirements:

1. Ask user for number of years of monthly rainfall to gather.

2. Use nested loops to gather the monthly data

3. The outer loop will iterate once for every year

4. The inner loop will iterate for 12 months which asks for inches of rain for each month.

5. Must pass the test cases below.    

6. Output style should be similar to test cases. Output should be clean and easy to read.

Test Cases:

How many years to gather data?: 2

Year: 1

Rainfall for month 1?: 1

Rainfall for month 2?: 2

Rainfall for month 3?: 3

Rainfall for month 4?: 4

Rainfall for month 5?: 5

Rainfall for month 6?: 6

Rainfall for month 7?: 7

Rainfall for month 8?: 8

Rainfall for month 9?: 9

Rainfall for month 10?: 10

Rainfall for month 11?: 11

Rainfall for month 12?: 12

Year: 2

Rainfall for month 1?: 1

Rainfall for month 2?: 2

Rainfall for month 3?: 3

Rainfall for month 4?: 4

Rainfall for month 5?: 5

Rainfall for month 6?: 6

Rainfall for month 7?: 7

Rainfall for month 8?: 8

Rainfall for month 9?: 9

Rainfall for month 10?: 10

Rainfall for month 11?: 11

Rainfall for month 12?: 12

Total Months: 24

Total Rainfall: 156.0

Average Rainfall :6.5

Explanation / Answer

#python 3

years = int(input('How many years to gather data?:'))     #prints on console and takes user input which is explicitly typecasted to integer

rainfall = []

for year in range(1,(years+1)): #1st year loop iteration

                print ("Year:",year)

                for month in range(1,13):                             #2nd month loop iteration

                                rain = int(input('Rainfall for month '+str(month)+'?:'))

                                rainfall.append(rain)                      #appends monthly rainfall to rainfall list

total_months = years*12

total_rainfall = sum(rainfall)        #sums all the values in rainfall list

Average_rainfall = total_rainfall/total_months if total_months != 0 else 0             #average rainfall while handling divide by zero exception.

print ('Total Months:',total_months)

print ('Total Rainfall:',total_rainfall)

print ('Average Rainfall:',Average_rainfall)