In: Computer Science
In Python:
Design a program that lets the user enter the total rainfall for each of the 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall, the months with the highest and lowest amounts. However, to start, do not ask the user for input. Instead, hard code the monthly rainfalls as a list in your program, and then use the list to determine how to conduct the processing and output described in the problem (total, average, highest and lowest).
Please write this short program in Python. Thank you.
NOTE :- I HAVE ATTACHED ALL THREE PROGRAMS IN A SEQUENCE WITH THE CODE, SCREENSHOTS OF THE OUTPUT AND SCREENSHOT OF THE CODE FOR INDENTATION RESPECTIVELY.
PROGRAM 1
rain_input=[25,1,14,15,5,151,144,126,25,10,5,121] #DECLARING THE STATIC RAINFALL VALUES #DECALRING THE MONTHS NAME months=["January","February","March","April","May","June","July","August","September","October","November","December"] total=sum(rain_input) #TOTAL RAIN CALCULATION OF THE YEAR BY SUM() FUNCTION avg=total/12 #AVERAGE OF ALL RAINFALL IN THE YEAS #DECALRING VARIABLES FOR MAX AND MIN RAIN max_rain=[] min_rain=[] #CALCULATING MAXIMUMN RAIN temp=rain_input[0] #BY DEFAULT THE FIRST RAINFALL WILL BE MAXIMUM #HERE WE ARE CHECKING IF THERE ARE TWO MONTHS WITH SAME RAINFALL THEN IT SHOULD PRINT BOTH THE MONTHS RATHER THAN ONLY PRINTING ONE MONTH for i in rain_input: #HERE INSIDE i WILL BE THE VALUE OF EACH RAINFALL OF THE YEAR if (i>=temp): #IT WILL CHECK IF i IS GREATER THAN TEMP THEN CHANGE TEMP TO MAX RAINFALL temp=i #SETTING THE MAXIMUM RAINFALL #IF THERE ARE SAME MONTHS WITH SAME RAINFALL THEN ADDING TO THE MAX_RAIN LIST THE INDEX OF THAT MONTH for i in range(len(rain_input)): if rain_input[i]==temp: #CHECKING IF THE MONTH VALUE IS SAME AS MAX max_rain.append(i) #IF YE THEN ADDING THE INDEX OF THAT MONTH TO MAX_RAIN LIST #CALCULATING MINIMUM RAIN temp=rain_input[0] #BY DEFAULT THE FIRST RAINFALL WILL BE MINIMUM #HERE WE ARE CHECKING IF THERE ARE TWO MONTHS WITH SAME RAINFALL THEN IT SHOULD PRINT BOTH THE MONTHS RATHER THAN ONLY PRINTING ONE MONTH for i in rain_input:#HERE INSIDE i WILL BE THE VALUE OF EACH RAINFALL OF THE YEAR if (i<=temp): #IT WILL CHECK IF i IS GREATER THAN TEMP THEN CHANGE TEMP TO MINIMUM RAINFALL temp=i #SETTING THE MINIMUM RAINFALL #IF THERE ARE SAME MONTHS WITH SAME RAINFALL THEN ADDING TO THE MIN_RAIN LIST THE INDEX OF THAT MONTH for i in range(len(rain_input)): if rain_input[i]==temp: #CHECKING IF THE MONTH VALUE IS SAME AS MINIMUM min_rain.append(i) #IF YE THEN ADDING THE INDEX OF THAT MONTH TO MIN_RAIN LIST print("Total Rain => ",total) #PRINTING THE TOTAL RAIN OF THE YEAR print("Average Rain => ",avg) #PRINTING THE AVERAGE RAIN OF THE YEAR print("\nThe Maximum Rainfall Was In The Following Months=") for i in max_rain: print(months[i]," => ",rain_input[i]) #PRINTING THE MONTHS WITH MAXIMUM RAIN AND THEIR VALUE print("\nThe Minimum Rainfall Was In The Following Months=") for i in min_rain: print(months[i]," => ",rain_input[i]) #PRINTING THE MONTHS WITH MINIMUM RAIN AND THEIR VALUE
OUTPUT OF THE PROGRAM
SCREENSHOT OF THE PROGRAM FOR INDENTATION
PROGRAM 2
#DECALRING A EMPTY LIST rain_input=[] print("Enter Rainfall=") for i in range(12): rain_input.append(int(input())) #TAKING INPUT FOR 12 MONTHS WITHOUT SPECIFYING THE MONTH NAME months=["January","February","March","April","May","June","July","August","September","October","November","December"] #DECALRING THE MONTHS NAME total=sum(rain_input) #TOTAL RAIN CALCULATION OF THE YEAR BY SUM() FUNCTION avg=total/12 #AVERAGE OF ALL RAINFALL IN THE YEAS #DECALRING VARIABLES FOR MAX AND MIN RAIN max_rain=[] min_rain=[] #CALCULATING MAXIMUMN RAIN temp=rain_input[0] #BY DEFAULT THE FIRST RAINFALL WILL BE MAXIMUM #HERE WE ARE CHECKING IF THERE ARE TWO MONTHS WITH SAME RAINFALL THEN IT SHOULD PRINT BOTH THE MONTHS RATHER THAN ONLY PRINTING ONE MONTH for i in rain_input: #HERE INSIDE i WILL BE THE VALUE OF EACH RAINFALL OF THE YEAR if (i>=temp): #IT WILL CHECK IF i IS GREATER THAN TEMP THEN CHANGE TEMP TO MAX RAINFALL temp=i #SETTING THE MAXIMUM RAINFALL #IF THERE ARE SAME MONTHS WITH SAME RAINFALL THEN ADDING TO THE MAX_RAIN LIST THE INDEX OF THAT MONTH for i in range(len(rain_input)): if rain_input[i]==temp: #CHECKING IF THE MONTH VALUE IS SAME AS MAX max_rain.append(i) #IF YE THEN ADDING THE INDEX OF THAT MONTH TO MAX_RAIN LIST #CALCULATING MINIMUM RAIN temp=rain_input[0] #BY DEFAULT THE FIRST RAINFALL WILL BE MINIMUM #HERE WE ARE CHECKING IF THERE ARE TWO MONTHS WITH SAME RAINFALL THEN IT SHOULD PRINT BOTH THE MONTHS RATHER THAN ONLY PRINTING ONE MONTH for i in rain_input:#HERE INSIDE i WILL BE THE VALUE OF EACH RAINFALL OF THE YEAR if (i<=temp): #IT WILL CHECK IF i IS GREATER THAN TEMP THEN CHANGE TEMP TO MINIMUM RAINFALL temp=i #SETTING THE MINIMUM RAINFALL #IF THERE ARE SAME MONTHS WITH SAME RAINFALL THEN ADDING TO THE MIN_RAIN LIST THE INDEX OF THAT MONTH for i in range(len(rain_input)): if rain_input[i]==temp: #CHECKING IF THE MONTH VALUE IS SAME AS MINIMUM min_rain.append(i) #IF YE THEN ADDING THE INDEX OF THAT MONTH TO MIN_RAIN LIST print("Total Rain => ",total) #PRINTING THE TOTAL RAIN OF THE YEAR print("Average Rain => ",avg) #PRINTING THE AVERAGE RAIN OF THE YEAR print("\nThe Maximum Rainfall Was In The Following Months=") for i in max_rain: print(months[i]," => ",rain_input[i]) #PRINTING THE MONTHS WITH MAXIMUM RAIN AND THEIR VALUE print("\nThe Minimum Rainfall Was In The Following Months=") for i in min_rain: print(months[i]," => ",rain_input[i]) #PRINTING THE MONTHS WITH MINIMUM RAIN AND THEIR VALUE
OUTPUT OF THE PROGRAM
SCREENSHOT OF THE PROGRAM FOR INDENTATION
PROGRAM 3
#DECALRING A EMPTY LIST rain_input=[] #DECALRING THE MONTHS NAME months=["January","February","March","April","May","June","July","August","September","October","November","December"] for i in range(12): rain_input.append(int(input("Enter Rainfall For "+months[i]+" => "))) #GETTING THE INPUT AND PRINTING THE MONTH NAME ALONGSIDE WITH IT. total=sum(rain_input) #TOTAL RAIN CALCULATION OF THE YEAR BY SUM() FUNCTION avg=total/12 #AVERAGE OF ALL RAINFALL IN THE YEAS #DECALRING VARIABLES FOR MAX AND MIN RAIN max_rain=[] min_rain=[] #CALCULATING MAXIMUMN RAIN temp=rain_input[0] #BY DEFAULT THE FIRST RAINFALL WILL BE MAXIMUM #HERE WE ARE CHECKING IF THERE ARE TWO MONTHS WITH SAME RAINFALL THEN IT SHOULD PRINT BOTH THE MONTHS RATHER THAN ONLY PRINTING ONE MONTH for i in rain_input: #HERE INSIDE i WILL BE THE VALUE OF EACH RAINFALL OF THE YEAR if (i>=temp): #IT WILL CHECK IF i IS GREATER THAN TEMP THEN CHANGE TEMP TO MAX RAINFALL temp=i #SETTING THE MAXIMUM RAINFALL #IF THERE ARE SAME MONTHS WITH SAME RAINFALL THEN ADDING TO THE MAX_RAIN LIST THE INDEX OF THAT MONTH for i in range(len(rain_input)): if rain_input[i]==temp: #CHECKING IF THE MONTH VALUE IS SAME AS MAX max_rain.append(i) #IF YE THEN ADDING THE INDEX OF THAT MONTH TO MAX_RAIN LIST #CALCULATING MINIMUM RAIN temp=rain_input[0] #BY DEFAULT THE FIRST RAINFALL WILL BE MINIMUM #HERE WE ARE CHECKING IF THERE ARE TWO MONTHS WITH SAME RAINFALL THEN IT SHOULD PRINT BOTH THE MONTHS RATHER THAN ONLY PRINTING ONE MONTH for i in rain_input:#HERE INSIDE i WILL BE THE VALUE OF EACH RAINFALL OF THE YEAR if (i<=temp): #IT WILL CHECK IF i IS GREATER THAN TEMP THEN CHANGE TEMP TO MINIMUM RAINFALL temp=i #SETTING THE MINIMUM RAINFALL #IF THERE ARE SAME MONTHS WITH SAME RAINFALL THEN ADDING TO THE MIN_RAIN LIST THE INDEX OF THAT MONTH for i in range(len(rain_input)): if rain_input[i]==temp: #CHECKING IF THE MONTH VALUE IS SAME AS MINIMUM min_rain.append(i) #IF YE THEN ADDING THE INDEX OF THAT MONTH TO MIN_RAIN LIST print("\nTotal Rain => ",total) #PRINTING THE TOTAL RAIN OF THE YEAR print("Average Rain => ",avg) #PRINTING THE AVERAGE RAIN OF THE YEAR print("\nThe Maximum Rainfall Was In The Following Months=") for i in max_rain: print(months[i]," => ",rain_input[i]) #PRINTING THE MONTHS WITH MAXIMUM RAIN AND THEIR VALUE print("\nThe Minimum Rainfall Was In The Following Months=") for i in min_rain: print(months[i]," => ",rain_input[i]) #PRINTING THE MONTHS WITH MINIMUM RAIN AND THEIR VALUE
OUTPUT OF THE PROGRAM
Note: If you have any queries then let me know in the comments. If you find it helpful then a Like would be appreciated