In: Computer Science
Write a Python 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
Hi,
Please find code below, all the line start with '#' are comments.
=============Code=================================
#Taking Year as a list. In which later, we will be appending the user input(rainfall in each month) year=[] #Declaring a list of months to track the rainfall in each month months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October","November","December" ] # Asking user to input the rainfall in each month. This will be the decimal value, hence we are taking it as float # Each line from line number 9 to 20, suggest user to enter rainfall in inches and we are saving it in variables jan=float(input('Please enter January rainfall in inches: ')) feb=float(input('Please enter February rainfall in inches: ')) mar=float(input('Please enter March rainfall in inches: ')) apr=float(input('Please enter April rainfall in inches: ')) may=float(input('Please enter May rainfall in inches: ')) jun=float(input('Please enter June rainfall in inches: ')) jul=float(input('Please enter July rainfall in inches: ')) aug=float(input('Please enter August rainfall in inches: ')) sep=float(input('Please enter September rainfall in inches: ')) oct=float(input('Please enter October rainfall in inches: ')) nov=float(input('Please enter November rainfall in inches: ')) dec=float(input('Please enter December rainfall in inches: ')) # Once we are done with the user input (rainfall in each month), we will append the above variables into the 'year' list, which # is declared in 1st line. # We have used 'extend' because we are combining the list with all the value entered by user # The below line suggest, We are extending our 'Year' list with the value user has entered year.extend((jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec)) # Now we are defining a total function which will calculate the total rainfall in a year # Using sum function to get the total of sum of the list year def total(): print('The total rainfall is ', sum(year),' inches') total() # Now we are defining a average function which will calculate the average rainfall in a year def average(): print('The average rainfall is', float(sum(year))/len(year), 'inches') average() # Now we are defining a highest function which will calculate the highest rainfall in a month # In the below function we have 1st retrieved the max value(highest rainfall) in the list(year) # Then we are used the max value( high variable) to find the index, and that index we are fetching in months list(declared in line 5) def highest(): high = max(year) month = months[year.index(high)] print('The highest rainfall is {max} inches in a month of {month}'.format(max=max(year),month=month)) highest() # Now we are defining a lowest function which will calculate the lowest rainfall in a month # This function works exactly like thw above function, except we are using min function to fetch minimum value in list def lowest(): low = min(year) month = months[year.index(low)] print('The lowest rainfall is {min} inches in the month of {month}'.format(min=min(year),month=month)) lowest() ================End======================================
=======================Screen prints( refer it for indentation)==================================
==============Please consider upvoting me, if this code helps you========Thanks=========