In: Computer Science
Write in Python and as 2 seperate programs
Write a program that allows the user to enter the total rainfall for each of 12 months into a LIST. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts. Data: January 7.9 inches February 10.1 inches March 3.4 inches April 6.7 inches May 8.9 inches June 9.4 inches July 5.9 inches August 4.1 inches September 3.7 inches October 5.1 inches November 7.2 inches December 8.3 inches
Write comments through both programs (file creation program, and display program) telling what the program is doing
Program 1
rainfall = [] #empty list to store the rainfall
#months list
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
#take rainfall data as input
for i in range(12):
data = float(input("Enter the rainfall in month {0}".format(i+1))) #take data for each month
rainfall.append(data) #append data in list
max_rainfall = max(rainfall) #calculate maximum rainfall
min_rainfall = min(rainfall) #calculate minimum rainfall
print("Total Rainfall = {0} inches".format(sum(rainfall))) #total rainfall - sum of all values in list
print("Average Rainfall = {0} inches".format(sum(rainfall)/12)) #average rainfall
#maximum rainfall with month, find the index with has the maximum value, and from months list display element at that index
print("Maximum Rainfall = {0} inches in {1} month".format(max_rainfall, months[rainfall.index(max_rainfall)]))
#minimum rainfall with month, find the index with has the minimum value, and from months list display element at that index
print("Lowest Rainfall = {0} inches in {1} month".format(min_rainfall, months[rainfall.index(min_rainfall)]))
Program 2
f1 = open('rainfall_data.txt', 'r') #opening data file in read mode
f2 = open('analysis.txt', 'w') #opening new file in write mode to write the results
rainfall = [] #empty list to store rainfall data
months = [] #list to store months
content = f1.readline() # read the content of the file
content = content.split() #split the file
for i in range(0, len(content)-2, 3):
months.append(content[i]) #store months into months list
rainfall.append(content[i+1]) #store values into rainfall list
rainfall = [float(ele) for ele in rainfall] #convert the values to float
total_rainfall = sum(rainfall) #calculate total rainfall
average_rainfall = total_rainfall/12 #calculate average rainfall
max_rainfall = max(rainfall) #calculate maximum rainfall
min_rainfall = min(rainfall) #calculate minimum rainfall
f2.write("Total Rainfall = " + str(total_rainfall) + " inches\n") #write total rainfall to file
f2.write("Average Rainfall = " + str(average_rainfall) + " inches\n") #write average rainfall to file
f2.write("Maximum Rainfall = " + str(max_rainfall) + " in " + str(months[rainfall.index(max_rainfall)]) + " month\n") #max rainfall
f2.write("Minimum Rainfall = " + str(min_rainfall) + " in " + str(months[rainfall.index(min_rainfall)]) + " month\n") #min rainfall
f1.close() #close first file
f2.close() #close second file
read the file in which results was written
f = open('analysis.txt', 'r') #open the file in which result has been written
print(f.read()) #read the content
f.close() #close the file
Screenshot
Program 1
Program 2
Output
Program 1
Program 2
Comments has been given in the code. Also, program as well as the output screenshot has bee attached for the reference.