SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Lists And Tuples
Exercise:
Programming Exercises
Question:3 | ISBN:9780132576376 | Edition: 2

Question

Rainfall Statistics
Design a program that lets the user 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 amounts.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Rainfall Statistics Python code:

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
          'August', 'September', 'October', 'November', 'December']
rainfall = []

# Ask user for rainfall input for each month
for month in months:
    rainfallData = float(input(f"Enter rainfall for {month}: "))
    rainfall.append(rainfallData)

# Calculate total and average rainfall
totalRainfall = sum(rainfall)
averageRainfall = totalRainfall / len(rainfall)

# Find month with highest and lowest rainfall
highestRainfall = max(rainfall)
lowestRainfall = min(rainfall)
highestMonth = months[rainfall.index(highestRainfall)]
lowestMonth = months[rainfall.index(lowestRainfall)]

# Display the results
print(f"\nTotal rainfall for the year: {totalRainfall} units")
print(f"Average monthly rainfall: {averageRainfall} units")
print(f"Month with the highest rainfall: {highestMonth} ({highestRainfall} units)")
print(f"Month with the lowest rainfall: {lowestMonth} ({lowestRainfall} units)")

Executed Output:

Enter rainfall for January: 90
Enter rainfall for February: 80
Enter rainfall for March: 75
Enter rainfall for April: 65
Enter rainfall for May: 55
Enter rainfall for June: 45
Enter rainfall for July: 90
Enter rainfall for August: 62
Enter rainfall for September: 78
Enter rainfall for October: 99
Enter rainfall for November: 11
Enter rainfall for December: 15

Total rainfall for the year: 765.0 units
Average monthly rainfall: 63.75 units
Month with the highest rainfall: October (99.0 units)
Month with the lowest rainfall: November (11.0 units)

 

0 0

Discussions

Post the discussion to improve the above solution.