Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive.
1.Use a file called randoms.txt as a input file and output file
a. If you go to open the file and its not there then ask the user to create the file
and then quit
2. Ask the user to specify how many random numbers the file will hold.
a.Make sure that the user input is > 1 and < 5000 inclusive.
b.Properly handle any bad input such as a floating point number, a negative number or String input.
c. If the user specifies bad input output an error message and ask the user to for input again.
3. Once the output is complete. Close the file and open it for reading.
a. Read all of the random number from that file and display:
i. The total of the numbers,
ii. the average of all the numbers,
iii. the largest and smallest numbers
iv. and the number of random numbers read from the
so far i have come up with the generator using the while loop:
here is the code:
import random random_numbers = open('randoms.txt', 'r') while True: try: qty_numbers = int(input('how many random numbers should be written to the file: ')) if qty_numbers < 0: Error = ValueError('Number should be between 1-500') raise Error print(qty_numbers) except ValueError: print('Please enter a valid number') else: if 0 < qty_numbers < 5001: valid = True random_numbers = open ('ran_numbers.txt','w') print('Your list of random numbers are: ') # create a loop to generate the random numbers in the quantity specified for count in range(qty_numbers): number = random.randint(1, 500) # print the list of random numbers print(number) # convert the numbers to a string and write them to the file random_numbers.write(str(number) + '\n') total = 0 count = 0 with open('randoms.txt', 'r') as f: for line in f: total += int(line.strip()) count += 1 print("Total of the numbers is " + str(total)) print("Number of random numbers read from the file is " + str(count))
the output is :
how many random numbers should be written to the file: 3
Your list of random numbers are:
407
175
267
Total of the numbers is 0
Number of random numbers read from the file is 0
how many random numbers should be written to the file:
import random
proceed=False
while not proceed:
proceed=True
try:
qty_numbers = int(input('how many random numbers should be written
to the file: '))
if qty_numbers<1 or qty_numbers>5000:
print('Please enter a number between [1,5000]')
proceed=False
except ValueError:
print('Please enter a valid number')
proceed=False
random_numbers = open ('ran_numbers.txt','w')
for count in range(qty_numbers):
number = random.randint(1, 500)
# print the list of random numbers
#print(number) #no need to print
# convert the numbers to a string and write them to the file
random_numbers.write(str(number) + '\n')
random_numbers.close()
print("Numbers were successfully written in the file. Now reading
it to show the required details...")
total = 0
count = 0
largest=1
smallest=500
with open('ran_numbers.txt', 'r') as f:
for line in f:
if (len(line.strip())>0):
num=int(line.strip())
total += num
count += 1
if num>largest:
largest=num
if num<smallest:
smallest=num
print("Total of the numbers is " + str(total))
print("Average of the numbers is " + str(total/count))
print("Largest of the numbers is " + str(largest))
print("Smallest of the numbers is " + str(smallest))
print("Number of random numbers read from the file is " +
str(count))
Write a program that writes a series of random numbers to a file. Each random number...
1.Write a python program that writes a series of random numbers to a file named random.txt. Each random number should be in the range of 1 through 300. The application should let the user specify how many random numbers the file will hold. 2. Write another program that reads the random numbers from the random.txt file, displays the numbers, then displays the following data: I. The total of the numbers II. The number of random numbers read from the file
Please use Python for this program Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range 1 through 500. The program should let the user specify how many random numbers to put into the file. The name of the file to write to should be 'random.txt'. Submit the random.txt file generated by your program with the assignment. Sample program execution: Python 3.4.3 Shell Eile Edit...
I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...
Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...
Write a program in c++ that generates a 100 random numbers between 1 and 1000 and writes them to a data file called "randNum.dat". Then re-open the file, read the 100 numbers, print them to the screen, and find and print the minimum and maximum values. Specifications: If your output or input file fails to open correctly, print an error message that either states: Output file failed to open Input file failed to open depending on whether the output or...
What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...
In C++ Sorted List of User Entered Numbers 2. Write a program that reads in a list of integers into a vector with base type int. Provide the facility to read this vector from an input file. Make sure to ask the user for a filename. The output is a two-column list. The first column is a list of the distinct vector elements. The second column is the count of the number of occurrences for each element. The list should...
C++ programming please Write a program that will display random numbers in order from low to high. 1. Declare an integer array of size 100. Ask the user how many numbers to work with (n). It can be less than 100. 2. Generate random numbers from 10 to 50. 3. Write the random numbers to an output file named random.dat. Close the file. 4. Open random.dat for input and read the data values into your array. Pass your array to...
Write a program that would ask the user to enter an input file name, and an output file name. Then the program reads the content of the input file, and read the data in each line as a number (double). These numbers represent the temperatures degrees in Fahrenheit for each day. The program should convert the temperature degrees to Celsius and then writes the numbers to the output file, with the number of day added to the beginning of the...
This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...