In: Computer Science
Write a program that asks the user to enter the total precipitation for each of the twelve months of the year into a list. The program should calculate and display the total precipitation for the year, the average monthly precipitation, and the months with the highest and lowest precipitation amounts. FYI: precipitation is measured in inches.
Assume that precipitation amounts are floating-point numbers. You will need a list to store the precipitation amounts and another list to store the names of the months.
Here is a sample run:
Enter total precipitation for January: 3.44 Enter total precipitation for February: 3.01 Enter total precipitation for March: 4.32 Enter total precipitation for April: 4.12 Enter total precipitation for May: 4.37 Enter total precipitation for June: 4.60 Enter total precipitation for July: 5.05 Enter total precipitation for August: 3.98 Enter total precipitation for September: 4.53 Enter total precipitation for October: 3.82 Enter total precipitation for November: 3.94 Enter total precipitation for December: 4.23 Total precipitation: 49.41 inches. Average precipitation: 4.12 inches. July has the highest precipitation: 5.05 inches. February has the lowest precipitation: 3.01 inches.
Notes:
PLEASE CODE IN PYTHON PROGRAMMING LANGUAGE
Code for the given output :-
# list for storing input values
precipitationList = []
# list of months
months = ["January","February","March","April","May","June","July",
"August","September","October","November","December"]
# Asking value for each month without using loop and appending in precipitationList
n = float(input("Enter total precipitation for {}: ".format(months[0])))
precipitationList.append(n)
n = float(input("Enter total precipitation for {}: ".format(months[1])))
precipitationList.append(n)
n = float(input("Enter total precipitation for {}: ".format(months[2])))
precipitationList.append(n)
n = float(input("Enter total precipitation for {}: ".format(months[3])))
precipitationList.append(n)
n = float(input("Enter total precipitation for {}: ".format(months[4])))
precipitationList.append(n)
n = float(input("Enter total precipitation for {}: ".format(months[5])))
precipitationList.append(n)
n = float(input("Enter total precipitation for {}: ".format(months[6])))
precipitationList.append(n)
n = float(input("Enter total precipitation for {}: ".format(months[7])))
precipitationList.append(n)
n = float(input("Enter total precipitation for {}: ".format(months[8])))
precipitationList.append(n)
n = float(input("Enter total precipitation for {}: ".format(months[9])))
precipitationList.append(n)
n = float(input("Enter total precipitation for {}: ".format(months[10])))
precipitationList.append(n)
n = float(input("Enter total precipitation for {}: ".format(months[11])))
precipitationList.append(n)
# Calculating sum of precipitation using sum function
total_precipitation = sum(precipitationList)
# Calculating Average by dividing sum with 12
average_precipitation = total_precipitation / 12
# Finding maximum precipitation using max funnction
maximum_precipitation = max(precipitationList)
# Finding minimum precipitation using min funnction
minimum_precipitation = min(precipitationList)
# getting month index by maximum and minimum precipitation using index function
max_month = precipitationList.index(maximum_precipitation)
min_month = precipitationList.index(minimum_precipitation)
print("\n")
print("Total precipitation: {} inches".format(total_precipitation))
print("Average precipitation: {:.2f} inches".format(average_precipitation))
print("{} has the highest precipitation: {} inches.".format(months[max_month],maximum_precipitation))
print("{} has the lowest precipitation: {} inches.".format(months[min_month],minimum_precipitation))
Screenshot for the above code :-
Output for the above code :-