Question

Write a program using the random number generator to create random rainfall measurements and save these...

Write a program using the random number generator to create random rainfall measurements and save these numbers in a text file. You will have more than 12 readings to cover more than 1 year of measurements. You may assume that we do not have more than 5 inches of rain in a month.

Your program will work with a text file of any number of months (you do not know how many readings are contained in the input file). Using the input data file, you will report the average amount of rain each year. You also need to consider if your input file contains incomplete full year data. For example, if there are 15 month readings, you should report the average rainfall in the first year, and the average of the last 3 months of the second year.

Your output should be clear and presentable


phython Language
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import random
#open the file in write mode
f = open("rainfall.txt", "w")
#save 15 numbers in it
for i in range(15):
   #create a random number between 0 to 5
   temp = round(random.random() * 5, 2)
   #store it in the file
   f.write(str(temp) + "\n")
#close the file
f.close()
#opent the file in read mode
f = open("rainfall.txt", "r")
#read the lines in file
lines = f.readlines()
#declare all the variables
months = 0
years = 0
average = 0
#iterate over the lines
for line in lines:
   #read the rainfall
   rain = float(line.strip("\n"))
   #add it to average
   average += rain
   months += 1
   if(months == 12):
       #if there are 12 months recored calculate the average
       average = round(average/12, 2)
       #increment years
       years += 1
       #display the average
       print("The average rainfall of " + str(years) + " year is " + str(average))
       #make months as 0
       months = 0
       #make the average as 0
       average = 0
#finally if there is any year which is not completed
if(months != 0):
   #calculate the average upto the months that read
   average = round(average/months, 2)
   #display the output
   print("The average rainfall of first " + str(months) + " months of " + str(years + 1) + " year is " + str(average))

Activities 2 Text Editor Wed 20:48 rainfall.py -Desktop - Open A Save = 60 rainfall.py rainfall.txt EN import random #open th

Activities 2 Text Editor Wed 20:48 rainfall.py -Desktop Open. A Save = 60 C rainfall.py rainfall.txt #Store L n the ille f.wr

Activities 2 Text Editor Wed 20:48 rainfall.txt -Desktop Open A Save 0 rainfall.py rainfall.txt 1.11 4.18 2.28 4.24 2.61 0.93

Activities Terminal Wed 20:44 deepika@deepika-TravelMate-P243-M: -/Desktop File Edit View Search Terminal Help deepika@deepik

Follow the images for indentation

If you have any doubts please comment and please don't dislike.

Add a comment
Know the answer?
Add Answer to:
Write a program using the random number generator to create random rainfall measurements and save these...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java language: Write a rainfall class that stores the total rainfall for each of 12...

    In Java language: Write a rainfall class that stores the total rainfall for each of 12 months of years 2015 -2017 into an 2D array of doubles. Use the text file at Actividades/Tareas link as input of rainfall data. The program should read the input data from a text file. The program should have methods that at least return the following: • Total rainfall for the years 2015 – 2017 - return a 1D array • The average monthly rainfall...

  • Write a program that scores the total rainfall for each of 12 months into an array...

    Write a program that scores the total rainfall for each of 12 months into an array double. A sample array definition is shown below double thisYear[] = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3, 2.4, 3.7}; The program should have the four functions to calculate the following 1. The total rainfall for the year 2. The average monthly rainfall 3. The month with most rain 4. The month with least rain Output: What to deliver? Your .cpp...

  • Your next programming assignment at the Research Center is to write a program that reads data...

    Your next programming assignment at the Research Center is to write a program that reads data from a file and produces a report of rainfall for that period of time. The program should read the starting month name from the data file, then read an ending month name, followed by reading in monthly rainfall amounts for each of the months from the starting month through the ending month. As it reads the monthly totals, it should sum (accumulate) the rainfall...

  • how to make this program for c++ visual studio 2015. Also, can I show your working...

    how to make this program for c++ visual studio 2015. Also, can I show your working and program. Also, Can you have notice for pseudo code? For the first problem, please implement Problem 4 on page 142 (p 143, 7E) of the text. A scan of the problem is provided below. This problem asks you to calculate the average rainfall for three months. The program should ask the user to enter the name of each month, such as June or...

  • [C++] Using Files—Total and Average Rainfall Write a program that reads in from a file a...

    [C++] Using Files—Total and Average Rainfall Write a program that reads in from a file a starting month name, an ending month name, and then the monthly rainfall for each month during that period. As it does this, it should sum the rainfall amounts and then report the total rainfall and average rainfall for the period. For example, the output might look like this: During the months of March–June the total rainfall was 7.32 inches and the average monthly rainfall...

  • Write a program that uses nested loops to collect data and calculate the average rainfall over...

    Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should prompt the user for the number of years. Be sure to ask for each months average rainfall for each year. The outer loop will iterate once for each year while the inner loop will iterate 12 times(one time per month) prompting for the months average rainfall on each iteration. Display the number of months, the total inches...

  • Write them in python IDLE ***** 5. Average Rainfall Write a program that uses nested loops...

    Write them in python IDLE ***** 5. 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,...

  • Write a C++ console application that allows your user to capture rainfall statistics. Your program should...

    Write a C++ console application that allows your user to capture rainfall statistics. Your program should contain an array of 12 doubles for the rainfall values as well as a parallel array containing the names of the months. Using each of the month names, prompt your user for the total rainfall for that month. The program should validate user input by guarding against rainfall values that are less than zero. After all 12 entries have been made, the program should...

  • Rainfall Statistics Write a program that lets the user enter the total rainfall for each of...

    Rainfall Statistics Write a program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November, December....

  • The Problem Design a program that lets the user enter the total rainfall for each of...

    The Problem Design a program that lets the user enter the total rainfall for each of 12 months into an array. The program should calculate and display the total rainfall for the year, the average monthly rainfall, the number of months above and below the average rainfall, and the highest rainfall for each quarter of the year. The Assignment Write a Java program that contains five method/functions as follows: main(). Calls the functions enterRain, rainStats, aboveBelow, and quarterlyRain. enterRain(). Uses...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT