In: Computer Science
In this programming challenge you are to create two Python programs: randomwrite.py and randomread.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 called randomwrite.py that writes a series of random integers 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 value 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:
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
Program 2: Random Number File Reader (randomread.py)
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 the string 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 the string 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.
import random # To generate random number
# Defines a function to receive a message as parameter
# Checks if the user entered number is positive then return the
number
# Otherwise display error message and ask again
def validNumber(message):
# Loops till positive number entered by the user
while(1):
# Accepts a number
number = int(input(message))
# Checks if the number is less than 0
if (number < 0):
# Displays error message
print("Number must be positive integer.")
# Continue the loop
continue
# Otherwise return the number
else:
return number
# Opens the file for writing
with open('randomnum.txt', 'w') as writeFile:
# Calls the function to accept number of times
times = validNumber("How many random numbers do you want? ")
# Calls the function to accept lower bound of random number
lower = validNumber("What is the lowest the random number should
be: ")
# Calls the function to accept upper bound of random number
upper = validNumber("What is the highest the random number should
be: ")
# Loops number of times entered by the user
for x in range(times):
# Generates random number between lower and upper bound
no = (random.randint(lower, upper))
# Converts the number to string
# Writes data to file
writeFile.write(str(no))
# Writes new line
writeFile.write("\n")
# Close the file
writeFile.close()
After execution of the above program randomnum.txt file contents
10
5
9
-----------------------------------------------------------------------------------------------------------
# Opens the file for reading
readFile = open('randomnum.txt', 'r')
# Checks if the mode is read mode
if readFile.mode == "r":
# Reads a number from the file and stores it in number
number = readFile.read()
# Displays the number
print(number)
# Close the file
readFile.close()
Sample Output:
How many random numbers do you want? -2
Number must be positive integer.
How many random numbers do you want? 3
What is the lowest the random number should be: -4
Number must be positive integer.
What is the lowest the random number should be: 5
What is the highest the random number should be: 10
10
5
9