Rainfall

\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{}^\circ}} % % Time \newcommand\second{\unit{s}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\feet{\unit{ft}} \newcommand\mile{\unit{mi}} \newcommand\mi{\mile} % % Volume \newcommand\gallon{\unit{gal}} % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Temperature \newcommand\celsius{\unit{^{\circ}C}} \newcommand\C{\unit{\celsius}} \newcommand\fahrenheit{\unit{^{\circ}F}} \newcommand\F{\unit{\fahrenheit}} \newcommand\kelvin{\unit{\K}} \newcommand\K{\unit{\kelvin}}\\% Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Torque \newcommand\ozin{\unit{oz}\text{-}\unit{in}} \newcommand\newtonmeter{\unit{N\text{-}m}}\end{aligned}\end{align} \]

Apr 16, 2025 | 258 words | 3 min read

3.2.3. Rainfall#

Write a Python 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. Format the precision of total and average rainfall to \(2\).

If the user enters less than \(1\) for the number of years, the program should display an invalid input message and stop. If the users enters a negative number for the rainfall of a month, the program should display an invalid input message, and ask for the rainfall for that month again until a non-negative value is entered.

Sample Output#

Use the values in Table 3.5 below to test your program.

Table 3.5 Test Cases#

Case

Years

Rainfall

1

0

2

1

1, 1, 1, -3, -4, -5, 1, 1, 1, -6, -7, -8, 1, 1, 1, 1, 1, 1

3

2

1.253, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3

Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.

Case 1 Sample Output

$ python3 rainfall_login.py Enter the number of years: 0 Invalid input; years must be greater than 0.

Case 2 Sample Output

$ python3 rainfall_login.py Enter the number of years: 1 For year No. 1 Enter the rainfall for Jan.: 1 Enter the rainfall for Feb.: 1 Enter the rainfall for Mar.: 1 Enter the rainfall for Apr.: -3 Invalid input; rainfall cannot be negative. Enter the rainfall for Apr.: -4 Invalid input; rainfall cannot be negative. Enter the rainfall for Apr.: -5 Invalid input; rainfall cannot be negative. Enter the rainfall for Apr.: 1 Enter the rainfall for May.: 1 Enter the rainfall for Jun.: 1 Enter the rainfall for Jul.: -6 Invalid input; rainfall cannot be negative. Enter the rainfall for Jul.: -7 Invalid input; rainfall cannot be negative. Enter the rainfall for Jul.: -8 Invalid input; rainfall cannot be negative. Enter the rainfall for Jul.: 1 Enter the rainfall for Aug.: 1 Enter the rainfall for Sep.: 1 Enter the rainfall for Oct.: 1 Enter the rainfall for Nov.: 1 Enter the rainfall for Dec.: 1 There are 12 months. The total rainfall was 12.00 inches. The monthly average rainfall was 1.00 inches.

Case 3 Sample Output

$ python3 rainfall_login.py Enter the number of years: 2 For year No. 1 Enter the rainfall for Jan.: 1.253 Enter the rainfall for Feb.: 3 Enter the rainfall for Mar.: 3 Enter the rainfall for Apr.: 3 Enter the rainfall for May.: 3 Enter the rainfall for Jun.: 3 Enter the rainfall for Jul.: 3 Enter the rainfall for Aug.: 3 Enter the rainfall for Sep.: 3 Enter the rainfall for Oct.: 3 Enter the rainfall for Nov.: 3 Enter the rainfall for Dec.: 3 For year No. 2 Enter the rainfall for Jan.: 3 Enter the rainfall for Feb.: 3 Enter the rainfall for Mar.: 3 Enter the rainfall for Apr.: 3 Enter the rainfall for May.: 3 Enter the rainfall for Jun.: 3 Enter the rainfall for Jul.: 3 Enter the rainfall for Aug.: 3 Enter the rainfall for Sep.: 3 Enter the rainfall for Oct.: 3 Enter the rainfall for Nov.: 3 Enter the rainfall for Dec.: 3 There are 24 months. The total rainfall was 70.25 inches. The monthly average rainfall was 2.93 inches.

Deliverables#

Save your finished program as rainfall_login.py, replacing login with your Purdue login. Then submit it along with all the deliverables listed in Table 3.6 below.

Table 3.6 Deliverables#

Deliverable

Description

rainfall_login.py

Your finished program.

Screenshot(s)

PNG(s) capturing all 3 test cases.