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:
Repetition Structures
Exercise:
Programming Exercises
Question:5 | ISBN:9780132576376 | Edition: 2

Question

Average Rainfall
Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Average Rainfall program code:

#Prompt and read the number of years from the user
numYears = int(input("Enter the number of years: "))

#Initialized the variables
totalRainfall = 0
numMonths = numYears * 12
#uses nested loops to collect data and 

for year in range(1, numYears + 1):
    for month in range(1, 13):
        rainfall = float(input(f"Enter the rainfall in inches for  {year} Year, Month {month}: "))
        totalRainfall += rainfall
#Calculate the average rainfall 
average_rainfall = totalRainfall / numMonths
#Display the number of months, 
#the total inches of rainfall, and 
#the average rainfall per month
print(f"\nTotal Number of months: {numMonths}")
print(f"Total inches of rainfall: {totalRainfall}")
print(f"Average rainfall per month: {average_rainfall:.2f} inches")

Executed Output:

Enter the number of years: 2
Enter the rainfall in inches for  1 Year, Month 1:20
Enter the rainfall in inches for  1 Year, Month 2: 10
Enter the rainfall in inches for  1 Year, Month 3: 4
Enter the rainfall in inches for  1 Year, Month 4: 3
Enter the rainfall in inches for  1 Year, Month 5: 5
Enter the rainfall in inches for  1 Year, Month 6: 8
Enter the rainfall in inches for  1 Year, Month 7: 9
Enter the rainfall in inches for  1 Year, Month 8: 10
Enter the rainfall in inches for  1 Year, Month 9: 44
Enter the rainfall in inches for  1 Year, Month 10: 11
Enter the rainfall in inches for  1 Year, Month 11: 22
Enter the rainfall in inches for  1 Year, Month 12: 25
Enter the rainfall in inches for  2 Year, Month 1: 55
Enter the rainfall in inches for  2 Year, Month 2: 2
Enter the rainfall in inches for  2 Year, Month 3: 3
Enter the rainfall in inches for  2 Year, Month 4: 44
Enter the rainfall in inches for  2 Year, Month 5: 5
Enter the rainfall in inches for  2 Year, Month 6: 6
Enter the rainfall in inches for  2 Year, Month 7: 33
Enter the rainfall in inches for  2 Year, Month 8: 23
Enter the rainfall in inches for  2 Year, Month 9: 22
Enter the rainfall in inches for  2 Year, Month 10: 21
Enter the rainfall in inches for  2 Year, Month 11: 11
Enter the rainfall in inches for  2 Year, Month 12: 22
Total Number of months: 24
Total inches of rainfall: 398.0
Average rainfall per month: 16.58 inches

 

0 0

Discussions

Post the discussion to improve the above solution.