Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Im in this computer class and im stuck on this certain assignment. The instructions are this: In this programming assignment you will create two python

Im in this computer class and im stuck on this certain assignment. The instructions are this:

In this programming assignment you will create two python programs. One will write a set of random numbers to a file. The second will read a set of random numbers from a file, count how many were read, display the random numbers, and display the total count of random numbers.

Random Number File Writer

Create a program called randomwrite.py that writes a series of random numbers to a file. The program is to request from the user how many random numbers to generate, the lower bound of the random numbers range, and the upper bound of the random numbers range. Each random integer number is to be on a separate line and is to be in the range of the inputted lower bound through the inputted upper bound. The file that the random numbers are written to is to be called randomnum.txt.

The user input is to be safe from crashes from invalid input. The user must enter a positive number for all required input (quantity of random numbers to generate, the lower bound of the random numbers range, and the upper bound of the random numbers range). If invalid input is received the user is to be given feedback and provided with the ability to enter a value again. A positive number is not zero and not negative. The program must not crash if an error occurs during a file operation. Use exception handling.

These are the same requirements for this program:

The program should accept user input for the quantity of random numbers to generate. The inputted number should be a positive integer.

The program should accept user input for the lower bound of the random numbers range. The inputted number should be positive integer.

The program should accept user input for the upper bound of the random numbers range. The inputted number should be positive integer.

User input may not cause the program to crash. Exceptions from invalid user input must be handled gracefully. The user is to be given feedback about invalid input and given the opportunity to supply the input without needing to run the program again.

The program should generate the user supplied amount of random integer numbers between the user supplied lower and upper bound range.

The random integers should be written to a file called txt with each random integer being on a separate line.

Example:

How many random numbers do you want? 10 What is the lowest the random number should be: 1 What is the highest the random number should be: 10 The random numbers were written to randomnum.txt

Random Number File Reader

Create a program called randomread.py that reads a series of random numbers from a file called randomnum.txt, counts how many there are, displays the random numbers, and displays the count.

The output to the user is to be labeled and nicely formatted. Prior to displaying the random numbers display List of random numbers in randomnum.txt: Each random number is to be displayed on a separate line. The count displayed to the user is to be preceded with Random number count:.

Example:

List of random numbers in randomnum.txt: 5 45 32 15 Random number count: 4

The program must not crash if an error occurs during a file operation. Use exception handling. For example, if randomread.py is run and there is no randomnum.txt file the program should handle the exception that occurs when attempting to open the file.

References

Chapter 5 Functions covers generating random numbers in section 5.7. Chapter 6 Files and Exceptions in your textbook covers how to write and read files. Writing and Reading Numeric Data in that chapter shows you how to write/read numbers to/from a file. Reading a File with a Loop and Detecting the End of the File has information on how to read each line in a file and know when all the lines have been read. Exceptions covers how try/except work and Programs 6-25, 6-26, and 6-27 show how to detect and handle an exception working with file operations.

Useful Information

If you open a file to write or read and you only specify the filename the location (directory/folder) will be assumed to be the same as the program opening the file.

Testing

Test randomwrite.py to make sure it properly handles invalid inputs and generates a file of the correct name that contains the quantity of random numbers requested.

Use randomwrite.py to generate randomnum.txt files that are then read by randomread.py and verify that that the information displayed is correct and properly formatted. Make sure that randomread.py doesnt crash when it is run and there is no randomnum.txt file present for it to open and read.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Issues In Database Management A Refernce For The Thinking Practitioner

Authors: Fabian Pascal

1st Edition

0201485559, 978-0201485554

More Books

Students also viewed these Databases questions