195k views
1 vote
Write a Python program named rainfall.py that asks the user to enter the total rainfall for each of 12 months and stores them in a list. After the totals have been entered the program will calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Be sure to label your output as in the example: Total rainfall: Average monthly rainfall: Month with highest rainfall: Month with lowest rainfall: 124.0 10.3 6 10

2 Answers

2 votes

Final answer:

The Python program named rainfall.py collects and stores monthly rainfall data in a list, calculates the total and average rainfall for the year, and identifies the months with the highest and lowest rainfall.

Step-by-step explanation:

The task requires writing a Python program called rainfall.py that collects the total rainfall for each month from the user, performs calculations, and displays the results. Below is a program that achieves this:

rainfall_data = []
for i in range(1, 13):
monthly_rainfall = float(input(f'Enter the total rainfall for month {i}: '))
rainfall_data.append(monthly_rainfall)

total_rainfall = sum(rainfall_data)
avg_rainfall = total_rainfall / 12
max_rainfall = max(rainfall_data)
min_rainfall = min(rainfall_data)

print(f'Total rainfall: {total_rainfall}')
print(f'Average monthly rainfall: {avg_rainfall:.1f}')
print(f'Month with highest rainfall: {rainfall_data.index(max_rainfall) + 1}')
print(f'Month with lowest rainfall: {rainfall_data.index(min_rainfall) + 1}')

This program first initializes an empty list to hold rainfall data. It then uses a loop to prompt the user for rainfall inputs 12 times. After gathering the data, it calculates the total rainfall for the year, average monthly rainfall, and identifies the months with the highest and lowest rainfall amounts.

User Andreasl
by
7.9k points
3 votes

Here is the Python program named rainfall.py that fulfills the requirements:

# Define an empty list to store monthly rainfall data

monthly_rainfall = []

# Loop through 12 months and collect user input for rainfall

for month_index in range(1, 13):

month_name = f"Month {month_index}"

rainfall = float(input(f"Enter the total rainfall for {month_name}: "))

monthly_rainfall.append(rainfall)

# Calculate total rainfall and average monthly rainfall

total_rainfall = sum(monthly_rainfall)

average_rainfall = total_rainfall / 12

# Find the month with the highest and lowest rainfall

highest_rainfall = max(monthly_rainfall)

lowest_rainfall = min(monthly_rainfall)

# Identify the month indexes for highest and lowest rainfall

highest_index = monthly_rainfall.index(highest_rainfall) + 1

lowest_index = monthly_rainfall.index(lowest_rainfall) + 1

# Display the results

print(f"Total rainfall: {total_rainfall:.1f}")

print(f"Average monthly rainfall: {average_rainfall:.2f}")

print(f"Month with highest rainfall: Month {highest_index} ({highest_rainfall:.1f} inches)")

print(f"Month with lowest rainfall: Month {lowest_index} ({lowest_rainfall:.1f} inches)")

User Sergey Zhigalov
by
9.2k points