Question

In: Computer Science

Write a Python program that uses function(s) for writing to and reading from a file: a....

Write a Python program that uses function(s) for writing to and reading from a file:

a. Random Number File Writer Function

Write a function that writes a series of random numbers to a file called "random.txt". Each random number should be in the range of 1 through 500. The function should take an argument that tells it how many random numbers to write to the file.

b. Random Number File Reader Function

Write another function that reads the random numbers from the file "random.txt", displays the numbers, then displays the following data:

  • The total of the numbers

  • The number of random numbers read from the file

c. Main Function

Write a main function that asks the user about how many random number the user wants to generate. It them calls the function in a. with the number the user wants as an argument and generates random numbers to write to the file. Next, it calls the function in b.

Make sure your program works correctly.

Solutions

Expert Solution

Python code pasted below.

import random
#random number file writer function
def random_writer(n):
fw=open("random.txt","w")
for i in range(n):
#generating random numbers
rand=random.randint(1,500)
#writing to output file
r=str(rand)+"\n"
fw.write(r)
fw.close()   
#random number file writer function
def random_reader(filename):
fr=open(filename,"r")
#initialize sum and count to 0
sum=count=0
while True:
line=fr.readline()
if not line:
break
else:
count=count+1
sum=sum+int(line)
fr.close()
print("Total number of numbers read from the file is ",count)
print("Sum of all random numbers in the file is ",sum)
#main program
#Reading how many random numbers to generate
n=int(input("How many random numbers to generate?"))
#calling random_writer() function
random_writer(n)
#calling random_reader function
filename="random.txt"
random_reader(filename)
Python code in IDLE pasted below for better understanding of the indent.

Output Screen

Output file generated


Related Solutions

Write a Python program that uses function(s) for the following problem: A nutritionist who works for...
Write a Python program that uses function(s) for the following problem: A nutritionist who works for a fitness club helps members by evaluating their diets. As part of her evaluation, she asks members for the number of fat grams and carbohydrate grams that they consumed in a day (two inputs, one for number of fat grams and the other for number of carb grams). Then, she calculates the number of calories that result from the fat, using the following formula:...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
What method is used to set up a file for reading and/or writing? code in python
What method is used to set up a file for reading and/or writing? code in python
Write a Python program which uses a function to calculate the perimeter of a rectangle. a...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a function named volume to calculate the volume of a cylinder volume = 3.14 x radius x radius x height .b function named volume to calculate the volume of a cuboid volume = Length x width x ht Write a Python Program to calculate the sum of all odd numbers for 2 to 20 using a for loop. 4. Write statements that assign random integers...
IN PYTHON: Write a program that displays the lines from the total.txt file in the following...
IN PYTHON: Write a program that displays the lines from the total.txt file in the following output. Use a try catch phrase to check for errors. Use only one function for this portion [main()]. Remember to use Python. Sample output below: 19 16, 29 3, 30 4, 34
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
(PYTHON) Write a program that does the following: reads each line from a txt file and...
(PYTHON) Write a program that does the following: reads each line from a txt file and convert it to lowercase counts the number of instances of: the characters 'a', 'e','i','o' and 'u' in the file creates a new file of file type .vowel_profile print outs lines in the file indicating the frequencies of each of these vowels Example input/output files: paragraph_from_wikipedia.txt (sample input) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.txt paragraph_from_wikipedia.vowel_profile (sample output) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.vowel_profile Please help!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT