PYTHON PROGRAMMING CODE THE PROGRAM BASED ON THE INSTRUCTIO

PYTHON PROGRAMMING - CODE THE PROGRAM BASED ON THE INSTRUCTIONS AND ALGORITHM PROVIDED

home / study / engineering / computer science / questions and answers / python programming - code the program based on ...

Your question has been answered! Rate it below.

Let us know if you got a helpful answer.

Question: PYTHON PROGRAMMING - CODE THE PROGRAM BASED ON THE...

Bookmark

PYTHON PROGRAMMING - CODE THE PROGRAM BASED ON THE INSTRUCTIONS AND ALGORITHM PROVIDED

Rainfall Algorithm

Declare and initialize variables and constants

int numYears, numMonths

float monthlyRain, totalRain, averageRain

Intro

while numYears < 1:

prompt for numYears

if numYears < 1:

    Display Invalid

for year in range(1,numYears + 1):

    for month range(1,13):

         while monthlyRain <= 0:

             Prompt for monthlyRain

             if monthlyRain <= 0:

                 Display Invalid

         Add to accumulator

         totalRain += monthlyRain

Calculate numMonths

numMonths = numYears * 12

Calculate averageRain

averageRain = totalRain / numMonths

Display numMonths, totalRain, averageRain

Average Rainfall Filenames: rainfallalg.pdf, rainfall.py write a program that uses nested 1oops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of ar. 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 tota inches of rainfall, and the average raintall per month for the entire period nput validation: Do not accept a number less than 1 for the number of years Do not accept negative numbers for the monthly rainfali. Example Output: e Python Shell Enter the number of year 2 :nv*11d. Monthly rainfall oan not be negstire. Enter he monthiy in fal: fer month 21 2 Enter the monthly zsin fel fes monEn 3 2 Eater Dse monthly zasn fai1 fez month 41 2 nc@ jtonshly rain f.21 tr month 6: 2 Enter the monthly :san fall for month 2 Entler the monthly rain fell for nonth 9: 2 Enter the monthly r3zn fall for month ::: 2 Enter the monthly zain tal2 toz month 22: 2.9 Enter the montnay eain tall tor month i2: 2.5 rnt.r the monthly tsin fall ter month 2 Enter the monthly rain tall for nantn s; 3 Enter the monthly rain fail torxanch Enter the monthly rais fall for monch s: 3 Entez the monchiy sain tall tor month 6: 9 Encez che monchly zoin raik tor nonth B: 3 Enter the monthly rain fall tor month 9 2 Ence: the monthly art t 11 to r month 10: 2 Encer the monchly zean fai1 or month 12 2 Enter the monchly rain fall tor month 12: 2 57.5 2.39583333333333ss Total Rainfaldz Ln: 45 Col: 4

Solution

# Variables need to be created to reference total rainfall, monthly rainfall, # average rainfall, the number of years, and the total number of months total_rainfall = 0.0 monthly_rainfall = 0.0 average_rainfall = 0.0 years = 0 total_months = 0 # Ask the user for the number of years years = int(input(\'Enter the number of years: \')) # Write a for loop and incorporate an accumulator variable to keep a running total for years in range(years): print(\'For year:\', years + 1) for month in range(12): monthly_rainfall = float(input(\'Enter the inches of rainfall for the month: \')) total_months += 1 # Add to the total rainfall month total_rainfall += monthly_rainfall # Calculate average rain fall for the total period average_rainfall = total_rainfall / total_months print(\'For\', total_months, \'months\') print(\'Total rainfall: \', format(total_rainfall, \'.2f\'), \'inches\') print(\'Average monthly rainfall: \', format(average_rainfall, \'.2f\'), \'inches\')
PYTHON PROGRAMMING - CODE THE PROGRAM BASED ON THE INSTRUCTIONS AND ALGORITHM PROVIDED home / study / engineering / computer science / questions and answers / p
PYTHON PROGRAMMING - CODE THE PROGRAM BASED ON THE INSTRUCTIONS AND ALGORITHM PROVIDED home / study / engineering / computer science / questions and answers / p

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site