410,022 views
19 votes
19 votes
Write a Python program (rainfall.py) to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. Then for each year, the program should ask twelve times, once for each month, for inches of rainfall for that month. At the end, , the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.

Your program should contain two functions:

(1) rainfall(year): This function takes in a year (integer) as a parameter, and returns two values (totalMonths and totalRainfall). In this function, you need to use nested loop. 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 inches (float) of rainfall for that month. After all iterations, the function should return the number of months (totalMonths) and the total inches of rainfall (totalRainfall). (Submit for 4 points)

(2) __main__: In the main, you do the following: (Submit for 6 points)

a. Prompt the user to input the number of years. Reprompt the user if the number of years is 0 or less. Hint: use a while loop.
b. Call rainfall(year) and pass to it the value entered above.
c. Calculate the average based on the returned values from rainfall function.
d. Display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.

User Renne
by
2.5k points

1 Answer

13 votes
13 votes

Answer:

def rainfall(year):

totalMonths = totalRainfall = 0

for y in range(year):

for month in range(12):

rainfall = float(input(f"Enter inches of rainfall for month #{month+1}: "))

totalRainfall += rainfall

totalMonths = year * 12

return totalMonths, totalRainfall

def __main__():

while True:

year = int(input("Enter the number of years: "))

if year > 0:

break

numberOfMonths, totalRainfall = rainfall(year)

averageRainFall = totalRainfall / numberOfMonths

print(f"\\Total number of months: {numberOfMonths}")

print(f"The total inches of rainfall: {totalRainfall}")

print(f"The average rainfall per month for the entire period: {averageRainFall}")

if __name__ == '__main__':

__main__()

Step-by-step explanation:

Create a function named rainfall that takes year as a parameter

Inside the function:

Create a nested for loop. The outer loop iterates for each year (The range is from 0 to year-1) and the inner loop iterates for each month of that year (The range is from 0 to 11). Inside the inner loop, ask the user to enter the rainfall for that month. Add the rainfall to the totalRainfall (cumulative sum)

When the loops are done, calculate the totalMonths, multiply year by 12

Return the totalMonths and totalRainfall

Inside the main:

Create a while loop that asks user to enter the number of years while it is greater than 0

Call the rainfall function, passing the year as parameter. Set the numberOfMonths and totalRainfall using the rainfall function

Calculate the averageRainFall, divide totalRainfall by numberOfMonths

Print the results

User Dave Aaron Smith
by
2.8k points