Westonci.ca is the Q&A platform that connects you with experts who provide accurate and detailed answers. Explore comprehensive solutions to your questions from knowledgeable professionals across various fields on our platform. Discover in-depth answers to your questions from a wide network of professionals on our user-friendly Q&A platform.

Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month.
After all iterations, the program chould display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period. (We are using class' with this challenge)
Input validation: Do not acept less than 1 for the number of years. Do not accept negative numbers for the monthly rainfall.
Output:
Enter the number of years: -2
Invalid. Enter 1 or greater: 2
Enter the rainfall, in inches, for each month.
Year 1 month 1: -3
Invalid. Enter 0 or greater: 3
Year 1 month 2: 5
Year 1 month 3: 7
Year 1 month 4: 9
Year 1 month 5: 6
Year1 month 6: 0
Year 1 month 7: 0
Year 1 month 8: 0
Year 1 month 9: 3
Year 1 month 10: 6
Year 1 month 11: 10
Year 1 month 12 8
Year 2 month 1: 7
Year 2 month 2: 6
Year 2 month 3: 10
Year 2 month 4: 8
Year 2 month 5: 4
Year 2 month 6: 0
Year 2 month 7: 0
Year 2 month 8: 0
Year 2 month 9: 4
Year 2 month 10: 15
Year 2 month 11: 12
Year 2 month 12: 5
Number of months: 24
Total rainfall: 128.0 inches
Average monthly rainfall: 5.333333333333333 inches (there are 15 3's)

Sagot :

Answer:

In Python:

year = int(input("Years: "))

while year<1:

   year = int(input("Invalid. Enter 1 or greater: "))

total = 0

for i in range(1,year+1):

   for j in range(1,13):

       month = int(input("Year "+str(i)+", Month "+str(j)+": "))

       while month<0:

           month = int(input("Invalid. Enter 0 or greater: "))

       total+=month

ave = total/(12*year)

print("Months: "+str(12 * year))

print("Total: "+str(total))

print("Average: "+str(ave))

Explanation:

This gets the number of years

year = int(input("Years: "))

This loop validates the number of years

while year<1:

   year = int(input("Invalid. Enter 1 or greater: "))

This initializes total to 0

total = 0

This iterates through the years

for i in range(1,year+1):

This iterates through the month of each year

   for j in range(1,13):

This gets the rainfall for each month

       month = int(input("Year "+str(i)+", Month "+str(j)+": "))

This loop validates the amount of rainfall

       while month<0:

           month = int(input("Invalid. Enter 0 or greater: "))

This calculates the total rainfall

       total+=month

This calculates the average rainfall

ave = total/(12*year)

This prints the number of month

print("Months: "+str(12 * year))

This prints the calculated total amount of rainfall

print("Total: "+str(total))

This prints the calculated average amount of rainfall

print("Average: "+str(ave))