110k views
4 votes
Design a program that lets the user enter the total rainfall for each of 12 months into an array. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Enhance the program so it sorts the array in ascending order and displays the values it contains. Name 'Search Modify the Sorted Names' program that you wrote for this exercise.

User Lomegor
by
4.8k points

1 Answer

3 votes

Answer:

rainfalls = []

for i in range(12):

rain = float(input("Enter rainfall: "))

rainfalls.append(rain)

total = 0

highest = rainfalls[0]

lowest = rainfalls[0]

highest_index = 0

lowest_index = 0

for i in range(len(rainfalls)):

total += rainfalls[i]

if rainfalls[i] >= highest:

highest = rainfalls[i]

highest_index = i

if rainfalls[i] <= lowest:

lowest = rainfalls[i]

lowest_index = i

average = total / 12

print("Total rainfall: " + str(total))

print("The average rainfall: " + str(average))

print("Highest: " + str(highest_index + 1))

print("Lowest: " + str(lowest_index + 1))

rainfalls.sort()

print(rainfalls)

Step-by-step explanation:

Create a list for rainfall values called rainfalls

Get the values from the user and put them in the rainfalls using a for loop

Create another for loop that iterates through the rainfalls. Add each value to the total. Find the highest and lowest month and their index using if else structure.

When the loop is done, calculate the average. Print the total, average and the index of the months with highest and lowest amount. Then, sort the list using sort() function and print the values

User SirViver
by
4.2k points