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.
USE Python 3, please type thanks! Write a Python 3 program to calculate the Body Mass...
USE Python 3, please type thanks! Write a Python 3 program to calculate the Body Mass Index (BMI) of a person if they enter their weight and their height to your program. Check out the formula here: http://www.freebmicalculator.net/calculate-bmi.php Your program should first print "Body Mass Index Calculator" The program will then ask the user if they want to enter Metric Units or English Units. Using the appropriate formula (see link above) calculate their BMI. Depending on their BMI show their...
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.
1. Write a python program to create a list of integers using random function. Use map...
1. Write a python program to create a list of integers using random function. Use map function to process the list on the expression: 3x2+4x+5 and store the mapped elements in another list. Now use filter to do sum of all the elements in another list. 2. Write a function that takes n as input and creates a list of n lists such that ith list contains first 10 multiples of i. 3. Write a function that takes a number...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT