Write a program that loads a list of integers from a file. The file is called nu
ID: 3758177 • Letter: W
Question
Write a program that loads a list of integers from a file. The file is called numbers.dat and it contains one integer per line. Write the program, complete with validation and exception handling, that displays the high value, low value, total number of values and the average. While it is possible to do this without using a list, I am requiring you to load the numbers (valid only) into a list and THEN process the data. You may use the built-in functions and methods available for lists.
Create your own data file.
Sample run if the file exists and contains 9 valid data items
>>>
9 items in the list
Lowest value: 1
Highest value: 12
Average: 6.0
Sample run if the file does not exits
>>>
error opening file
The file exits, but is emtpy
>>>
0 items in the list
No data in the list
Sample run if the file exists and contains 3 data items, 2 of which are not valid. Program ignores invalid data and proceeds.
>>>
error in value, not added
error in value, not added
1 items in the list
Lowest value: 12
Highest value: 12
Average: 12.0
PLEASE DO IT IN PYTHON PLEASE!
Explanation / Answer
import numpy in np
def readFile():
arr = []
inp = open ("number.dat","r")
#read line into array
for line in inp.readlines():
if isinstance( line, int )
arr.append(line)
else
print "error in value, not added"
print "Lowest value:" + min(arr)
print "Highest value:" + max(arr)
print "Average value:" + np.average(arr)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.