Question

Write a program that writes a series of random numbers to a file. Each random number...

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:

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

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))

Add a comment
Know the answer?
Add Answer to:
Write a program that writes a series of random numbers to a file. Each random number...
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
  • 1.Write a python program that writes a series of random numbers to a file named random.txt....

    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...

    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...

  • Having some trouble on this Python problem. It has 2 parts. Part 1: Random Number file...

    Having some trouble on this Python problem. It has 2 parts. Part 1: 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 100 through 500. The application should let the user specify how many random numbers the file will hold. sample Outputs Enter the name of the file to which results should be written: ran_numbers_dude.txt Enter the number of random numbers to be...

  • I need help in Python. This is a two step problem So I have the code...

    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...

  • Write a program in c++ that generates a 100 random numbers between 1 and 1000 and...

    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...

  • Description: Create a program called numstat.py that reads a series of integer numbers from a 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...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    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...

    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...

  • Write a program in C++ that, given a seven-digit number, writes to a file every possible...

    Write a program in C++ that, given a seven-digit number, writes to a file every possible seven-letter corresponding to that number. There are 2187 (3 to the seventh power) such words. Include the numbers 0 and 1; just embed them in the words as a 0 and a 1. Example: one possibility for 5701679 would be: LR01OPW. Assume the user correctly enters only 7 digits (no ‘-‘). Read the user input into a char array. Use a recursive function to...

  • I need help writing these 2 programs please include all the indents :) In this programming...

    I need help writing these 2 programs please include all the indents :) In this programming challenge you are to create two Python programs: randomwrite.py andrandomread.py. One program, randomwrite.py, is to write a set of random numbers to a file. The second program, randomread.py, is to read a set of random numbers from a file, counts how many were read, displays the random numbers, and displays the total count of random numbers. Random Number File Writer (randomwrite.py) Create a program...

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