In this programming assignment you are to create a program called numstat.py tha
ID: 3854924 • Letter: I
Question
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 minimum value. The range of the values. The range is the maximum value minus the minimum value. The program is to prompt the user for the name of the file that contains the numbers. If an exception occurs trying to open or read the file an error message is to be displayed. The program is not to crash if the file is not found or there is an error reading the file. Use try-except!
Explanation / Answer
try:
filename = input("Please enter your file name: ")
sum= 0
avg=0;
counter=0;
openthefile = open(filename, "r")
print(filename); #prints the name of the file
for i in openthefile:
counter+1 #counts number of numbers in the file
sum+=float(i)
print (sum); #prints sum of numbers in the file
print (counter); #prints how many numbers in the file
print (sum/counter); #prints the average of numbers
print (max(infile)); #prints the maximum value in the file using Python3
print (min(openthefile)); #prints the minimum value in the file using Python3;
print (max(openthefile))-(min(openthefile)); # prints the range
except FileNotFoundError:
print("Your File Name: "+filename);
print("File Not Found"); #prints if the file is not found
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.