Question

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 written to the file: 15

15 random numbers have been written into the output file.

another sample output...

Enter the name of the file to which results should be written:

ran_numbers_johnson.txt

Enter the number of random numbers to be written to the file: 90

90 random numbers have been written into the output file.

Part 2: Average of Numbers with exception handling

Assume that a file containing a series of integers is named numbers.txt and exists on the computers disk. Download this file which is attached. Write a program that calculates the average of all the numbers stored in the file. Make sure that the program should ask a file name to read. (I don't know how to attach the file to this post, so ill just paste the contents of that file below)

22
14
39
109
87
-99
333
78
65
76
292
282
239
cat
392
98
786
981
388
-987
-12
-45
383
381
1
23
40
dog

Modify the program that you wrote so it handles the following exceptions:

1. It should handle any IOError exceptions that are raised when the file is opened and data is read from it.

2.It should handle any ValueError exceptions that are raised when the items that are read from the file are converted to a number.

Check and fix the file by removing non-numeric data. There are two non-numeric data 'cat' and 'dog'.

sample Outputs

Enter a file name: num.txt (file name is wrong)

An error occurred while trying to read the file. No such file exists.

Before fixing the file:

Enter a file name: numbers.text

Non-numeric data found in the file.

After fixing the file:

Enter a file name: numbers.txt

Average: 152.54

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

import random

file_name = input("Enter the name of the file to which results should be written: ")

n = int(input("Enter the number of random numbers to be written to the file: "))

output = open(file_name,"w")

for i in range(n):

output.write(str(random.randint(100,501)))

output.write("\n")

output.close()

print("{} random numbers have been written into the output file.".format(n))

file_name = input("Enter a file name: ")

try:

read = open(file_name,"r")

#read all lines into list

values = read.readlines()

try:

values = [int(x) for x in values]

s = sum(values)

avg = s/len(values)

print("Average: {:.2f}".format(avg))

except ValueError:

print("Non-numeric data found in the file.")

except FileNotFoundError:

print("An error occurred while trying to read the file. No such file exists.")

Add a comment
Know the answer?
Add Answer to:
Having some trouble on this Python problem. It has 2 parts. Part 1: Random Number file...
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
  • Starting out with python 4th edition Write program that lets the user enter in a file...

    Starting out with python 4th edition Write program that lets the user enter in a file name (numbers.txt) to read, keeps a running total of how many numbers are in the file, calculates and prints the average of all the numbers in the file. This must use a while loop that ends when end of file is reached. This program should include FileNotFoundError and ValueError exception handling. Sample output: Enter file name: numbers.txt There were 20 numbers in 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...

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

  • please read directions first. this is supposed to be a hybrid programe add comments please JAVA...

    please read directions first. this is supposed to be a hybrid programe add comments please JAVA please add comments Programming Project 1 and Programming Project 2 "Hybrid" Your goal is to write a program that combines the functionality of both Programming Project 1andProgramming Project 2 into a single program. That is, your program should read a file ( Numbers.txt) of numbers of type double that contains some duplicates and is ordered smallest to largest. Your program should ignore the duplicate...

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

  • java Part 1 Create a NetBeans project that asks for a file name. The file should...

    java Part 1 Create a NetBeans project that asks for a file name. The file should contain an unknown quantity of double numeric values with each number on its own line. There should be no empty lines. Open the file and read the numbers. Print the sum, average, and the count of the numbers. Be sure to label the outputs very clearly. Read the file values as Strings and use Double.parseDouble() to convert them. Part 2 Create a NetBeans project...

  • Need some help on this Python Problem called "unique words" the text.txt file can be any...

    Need some help on this Python Problem called "unique words" the text.txt file can be any .txt file with words in it. Write a program that opens a specified text file and then displays the number of unique words in the text file after stripping all the white spaces, comma, and the punctuation marks and list of all the unique words found in the file. The list should be sorted in alphabetical order. The program should handle the ‘file not...

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

  • 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

  • Write a program that would ask the user to enter an input file name, and an...

    Write a program that would ask the user to enter an input file name, and an output file name. Then the program reads the content of the input file, and read the data in each line as a number (double). These numbers represent the temperatures degrees in Fahrenheit for each day. The program should convert the temperature degrees to Celsius and then writes the numbers to the output file, with the number of day added to the beginning of the...

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