Question: I required to write an algorithm for these codes. I forgot how to write it. This is the question: Write a program that uses nested
I required to write an algorithm for these codes. I forgot how to write it.
This is the question:
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. Years less than one or greater than ten must be rejected. The outer loops 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 should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period. (hint: the number of years must be validated not to enter less than 1 or more than 10 years. Also, you must initialize variables at the beginning of the programs)
These are the codes. They are properly working, I just need to write the algorithm
#variable initialization m = 0 Sum = 0 #accept number of years Y = int(input("Enter number of years: ")) #validated the years are in the range 1 to 10 or not while Y < 1 or Y > 10: Y = int(input("Enter number of years: ")) #if year are not in the range continue loop if Y < 1 or Y > 10: print("Invalid years..!!!") #loop for number of years for i in range(Y): #print year print("Enter Rainfall in year", i+1," : ") #loop for 12 times for i in range(12): #enter rainfall for every month print("Enter Rainfall in month", i+1, ": ", end =' ') V = float(input()) #calculate total rain fall Sum = Sum + V #calculate total month m = m + 1 #calculate average rainfall per month Avg = Sum/m #display total rainfall print("Total rainfall = ", Sum) #display average rainfall print("Average rainfall per month = ", Avg)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
