Question

In: Computer Science

Please use Python 3 3). Write a function that writes a series of random numbers to...

Please use Python 3

3). Write a function that writes a series of random numbers to a text file named ‘random_number.txt’. Each random number should be in the range of 1 through 500. The function should let the user specify how many random numbers the file will hold. Then write another function that reads the random numbers from the ‘random_number.txt’ file, displays the numbers, and then displays the total of the numbers and the number of random numbers read from the file. This means, you will have two functions, the first one being the file writing function and the second one being the file reading function.

Sample input & output:

Output for the first file writing function should be that you have created a txt file in your computer containing the generated random numbers. Output when you call the file reading function (e.g., I generate 5 numbers):

(Output) 36

(Output) 379

(Output) 69

(Output) 302

(Output) 301

(Output) The total of the numbers is 1087

(Output) We have read 5 numbers from the file.

Solutions

Expert Solution

Attached the code and smaple outputs. Please Upvote

First File, to create the Txt file and add the numbers

import random

file = open('test.txt','w')

count = int(input("How many numbers should be written to the file? "))

for i in range(count):
    file.write(str(random.randint(1,500)))
    file.write(" ")
file.close()
print("File test.txt created with {} Numbers".format(count))

Second file to read the file and output the sum of the numbers

fl = open('test.txt','r')

content = fl.read()
content = content.split()

sum = 0

for i in content:
    sum += int(i)
    print(i)

            
print("The total of the numbers is {}".format(sum))
print("We have read {} numbers from the file".format(len(content)))

Related Solutions

1. Writing a Random Temperature File Write a function that writes a series of random Fahrenheit...
1. Writing a Random Temperature File Write a function that writes a series of random Fahrenheit temperatures and their correspond- ing Celsius temperatures to a tab-delimited file. Use 32 to 212 as your temperature range. From the user, obtain the following: • The number of temperatures to randomly generate.• The name of the output file. A sample run is included below (you must follow the format provided below): Please enter the name of your file: Example.txt Please enter the number...
Use python. redact_file: This function takes a string filename. It writes a new file that has...
Use python. redact_file: This function takes a string filename. It writes a new file that has the same contents as the argument, except that all of the phone numbers are redacted. Assume that the filename has only one period in it. The new filename is the same as the original with '_redacted' added before the period. For instance, if the input filename were 'myfile.txt', the output filename would be 'myfile_redacted.txt'. Make sure you close your output file.
Write a program in Python that will print first 100 numbers of the following series: 0,...
Write a program in Python that will print first 100 numbers of the following series: 0, 1, 1, 2, 3, 5, 8……..
Solve please in python b) Create a program that shows a series of numbers that start...
Solve please in python b) Create a program that shows a series of numbers that start at a and increase from 5 to 5 until reaching b, where a and b are two numbers captured by the user and assumes that a is always less than b. Note that a and b are not necessarily multiples of 5, and that you must display all numbers that are less than or equal to b. c) Create a program that displays n...
Write a function that will generate an array of random numbers. It needs to:
DO IN C++Write a function that will generate an array of random numbers. It needs to:Take 3 integers as parameters-The first is the minimum value-the second is the maximum value-the third is the size of the new array-create a dynamically allocated array of the correct size-generate a random number (between min and max) for each element in the array-return a pointer to the arrayCreate a main() function that tests the above function and displays the values in the random array.
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
In python of Jupiter notebook Write a python function called trng that takes three numbers x,...
In python of Jupiter notebook Write a python function called trng that takes three numbers x, y, and z, and specifies if those can form a triangle (i.e., returns the word triangle if they can, and Not a triangleotherwise). Note: In order for three numbers to form a triangle sum of any two of them must be greater than the third one (e.g., x=1, y=2, z=4 cannot form a triangle because x+y is not greater than z even though x+z>y...
1. Write a python function that receives two positive numbers and displays the prime numbers between...
1. Write a python function that receives two positive numbers and displays the prime numbers between them.Note: A prime number (or a prime) is a natural number greater than 1 and that has no positive divisors other than 1 and itself. 2. Using either Whileor Foriteration loops, write a python code that prints the first Nnumbers in Fibonacci Sequence, where N is inputted by the user. Now, revise this code to instead print a) the Nthnumber in Fibonacci Sequence.b) the...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name it addToDictionary(s,r) that take a string and add it to a dictionary if the string exist increment its frequenc 2) function named freq(s,r) that take a string and a record if the string not exist in the dictinary it return 0 if it exist it should return its frequancy.
Please write in Python code please Write a program that creates a dictionary containing course numbers...
Please write in Python code please Write a program that creates a dictionary containing course numbers and the room numbers of the rooms where the courses meet. The dictionary should have the following key-value pairs: Course Number (key) Room Number (value) CS101 3004 CS102 4501 CS103 6755 NT110 1244 CM241 1411 The program should also create a dictionary containing course numbers and the names of the instructors that teach each course. The dictionary should have the following key-value pairs: Course...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT