Question

In: Computer Science

In this programming challenge you are to create two Python programs: randomwrite.py and randomread.py. One program,...

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:

  • 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 called randomnum.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

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.

Solutions

Expert Solution

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


Related Solutions

C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
Python Programming Revise the ChatBot program below. There needs to be one list and one dictionary...
Python Programming Revise the ChatBot program below. There needs to be one list and one dictionary for the ChatBot to use. Include a read/write function to the program so that the program can learn at least one thing and store the information in a text document. ChatBot program for revising: # Meet the chatbot Eve print('Hi there! Welcome to the ChatBot station. I am going to ask you a series of questions and all you have to do is answer!')...
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts...
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts a Celsius temperature to Fahrenheit. Modify that program so it uses a loop to display a table of the Celsius temperatures 0–20, and their Fahrenheit equivalents. Display table on screen and it a .txt file. c++
#Sphere – pi and radius Create two python programs named program_1 and program_2 that will calculate...
#Sphere – pi and radius Create two python programs named program_1 and program_2 that will calculate the surface area of a sphere. The programs will use user-defined functions. Global variables are NOT allowed in either program. Program_1 will be listed first in the answer box with Program_2 written below it. Separate the programs with a line of dashes ( --------------------------------------------- ) as shown in the example below. Program_1 will: Prompt the user for the radius of the shape and store...
in python please Q1) Create a Singly link list and write Python Programs for the following...
in python please Q1) Create a Singly link list and write Python Programs for the following tasks: a. Delete the first node/item from the beginning of the link list b. Insert a node/item at the end of the link list c. Delete a node/item from a specific position in the link list Q2) Create a Singly link list and write a Python Program for the following tasks: a. Search a specific item in the linked list and return true if...
Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program:...
Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program: Your program will create a username of your choice using a Kali Linux command "useradd -m mark", then set the password for that user using the Kali Linux Command "echo "mark:10101111" | chpasswd". Then you create a dictionary file using the Kali Linux command "crunch 8 8 01 > mylist.txt" Your python script should crack the password for that user and display on the...
The programming language is Python Instructions: Create a function that will delete a node in a...
The programming language is Python Instructions: Create a function that will delete a node in a Linked List based on position number. On below example, if you want to delete position #2, it will remove the Banana (arrangement of nodes below is Apple, Banana, Cherry, Grapes, Orange). myLinkedList = LinkedList() myLinkedList.append("Banana") myLinkedList.append("Cherry") myLinkedList.append("Grapes") myLinkedList.append("Orange") myLinkedList.prepend("Apple") myLinkedList.deleteByPositionNum(2) node = myLinkedList.head while node: print(node.value, " ") node = node.next_node You may start with the function head: def deleteByPositionNum(self, positionNum):
Programming Python Jupiter notebook Write a Python program that gets a numeric grade (on a scale...
Programming Python Jupiter notebook Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range, it asks him/her to enter a numeric input in the correct range, instead of returning an error. Example: Enter your score: 78 Letter...
This is an exercise to design and write a Python program in good programming style for...
This is an exercise to design and write a Python program in good programming style for a simulation of stock price over a period of 100 days. In this exercise, you are asked to simulate the stock price starting at $100.00 for 100 days with a daily fluctuation based on the Normal Distribution with mean = 0.0 & sigma = 0.0125. The program will show the daily stock price, the 7-day minimum, the 7-day maximum, the 7-day average, and the...
can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT