Question

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

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

Answer : please up vote or comment for any query i will update the same .Thanks

Note : check images for indentation and output

Program Plan : randomwrite.py

1. ran three loop to take input RandomNumber,Lower Bound,UpperBound and check in while loop for validation

2. now open a file generate random number and write to write in a new line

3. handle exception

Program plan : randomread.py

1. open a file and hand;e file opening exceptions

2. read line by line and check for integer and increase count by 1

3. display number of count

Program : randomwrite.py

import random
RandomNumber=0
LowerBound=0
UpperBound=0
while(RandomNumber<=2): #get user input till random number is less than 3
RandomNumber=input("How many random numbers do you want? ")


while(LowerBound<=0): #lower random number should be greater than 0
LowerBound=input("what is the lowest random number should be? ")

while(UpperBound<=LowerBound): #upper number should greater than lower
UpperBound=input("what is the highest random number should be? ")

i=0
try:
fh = open("randomnum.txt", "w") #open
while i in range(RandomNumber): # zero to number of random number
RandomNum=random.randint(LowerBound,UpperBound) #range between lower and upper
fh.write(str(RandomNum)) #convert int to string and write
fh.write("\n") #new line
i+=1
  

except IOError: #throw exception
print "Error: can\'t find opening error"
else:
print "Written content in the file successfully"
fh.close()

Program : randomread.py

import os
count=0
fname="randomnum.txt" #file name
if os.path.exists(fname): #check file exist or not
with open(fname, 'rb') as f: #open fle

try:

count = 0
for line in f: #read line by line
try:
if(int(line)):
count += 1 #if line have integer increase count
except ValueError: #throw exception integer not found
pass
else:
pass

except IOError:
print ("Could not read file:", fname) #unable to read
else:
print("Random number count : ",count)
else:
print("file not found")
  
  


Output Images :


Add a comment
Know the answer?
Add Answer to:
I need help writing these 2 programs please include all the indents :) In this programming...
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
  • Could anyone please help with this Python code assignment? In this programming assignment you are to...

    Could anyone please help with this Python code assignment? In this programming assignment you are to create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The count of how many numbers are in the file. The average of the numbers. The average is the sum of the numbers divided by how many there are. The maximum value. The...

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

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

  • Follow the directions in the TODO statements of RandomGenerator.java (attached). Submit your altered RandomGenerator.java file. public...

    Follow the directions in the TODO statements of RandomGenerator.java (attached). Submit your altered RandomGenerator.java file. public class RandomGenerator { public static void main(String[] args) { //TODO: Create a Scanner object to read keyboard input. //TODO: Ask the user a lower and upper bound. //TODO: Ask the user for the number of random integers to generate. //TODO: Generate random integers of the desired length and bound. //TODO: Print the average, max, min, count, and sum of the integers. } } 1....

  • Write a program in C++. You need everything everythihng given You will create a function that...

    Write a program in C++. You need everything everythihng given You will create a function that validates input. It should accept only non-negative integers. If the input is incorrect the function should throw an exception. The exception will not be caught in the function. You will create a function that reads in 2 integer values. One is a number that will be raised to the power of the second. So if 5 and 6 are entered the result would be...

  • I need help with this python programming exercise, please! thanks in advance Create a Python script...

    I need help with this python programming exercise, please! thanks in advance Create a Python script file called hw4.py. Add your name at the top as a comment, along with the class name and date. Both exercises should be in this file, with a comment before each of them to mark it. Ex. 1. Write a program that inputs an integer number from the user, then prints a letter "O" in ASCII art using a width of 5 and the...

  • C++ programming language , I need help writing the code . Please explain if you can...

    C++ programming language , I need help writing the code . Please explain if you can . Exercise 10: Unit conversion (10 points) Write a console program that converts meters into feet and inches. The program starts by asking the user for a measurement in meters. The program should accept a floating point number. After getting the number, the program computes the number of feet and inches equal to the specified number of meters. The program outputs feet and inches...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

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