Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

numbers 728 44 591 586 660 852 554 419 451 698 947 660 170 396 627 975 748 13 37

ID: 3843103 • Letter: N

Question

numbers

728
44
591
586
660
852
554
419
451
698
947
660
170
396
627
975
748
13
373
881
112
266
500
476
951
433
409
786
592
251
324
930
148
785
101
929
583
505
487
175
995
875
913
620
444
806
259
8
893
637
635
238
140
989
476
438
835
641
650
904
268
798
464
716
918
377
332
691
981
370
747
217
668
777
475
322
362
540
209
300
884
245
375
400
915
331
765
522
222
849
907
461
507
976
575
671
755
901
263
542

numbersg80.01
65.62
96.41
97.02
97.24
94.1
88.49
75.87
95.16
82.02
93.69
95.38
96.35
96.41
100
96.06
94.74
85.3
100
80.67
92.6
97.27
94.2
94.62
93.49
82.34
96.35
0
72.1
95.27
87.51
94.74
88.82
98.14
87.79
85.39
94.29
95.45
91.92
93.9
95.03
94.2
90.36
94.02
96.18
86.52
97.6
87.77
90.73
94.26
94.8
94.6
97.86
96.94
93.72
84.68
96.33
87.82
95.64
78.58
90.59
94.67
82.62
97.12
95.95
83.91
95.58
98.56
92.51
89.33
74.46
95.98
87.74
88.51
93.65
93.35
0
77.9
91.82
88.18
95.35
95.41
96.83
94.72
97.02
92.5
94.57
96.22
96.89
99.58
99.17
89.55
48.94
97.44
89.96
94.87
95.03
96.06
84.96
94.9
89.71
95.03
83.08
92.88
95.16
98.56
92.12
73.48
99.1
97.72
92.4
96.41
94.24
86.92
86.47
73.41
73.38
77.63
98.57
95.62
84.93
98.58
95.26
96.19
87.49
86.58
95.45

Number Stats Programming Assignment

In this programming assignment you are to create a program called numstat.py that reads a seriesof float 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 grade distribution of the values (How many A’s, B’s, C’s, D’s, F’s. eg anythingabout 90 is an A)The program is to prompt the user for the name of the file that contains the numbers. If anexception occurs trying to open or read the file an error message is to be displayed. The programis not to crash if the file is not found or there is an error reading the file. Use try-except!You are NOT allowed to use max(), min(), sum(), or len() in getting any of these values.The output from the program is to display the information described above using the followingstrings preceding the values. There is to be a space between the colon and the value. For this “Grade Distribution” case I have a file of grades that I want stats on. If the range of thenumbers Max =< 100 and Min >= 0 then execute the grade distribution. For example, if I have afile with numbers that range from -100 -> 1000 I’m unlikely trying to calculate grades.

Red Only prints when The numbers range from 0-100

File name: Sum: Count: Average: Maximum: Minimum: Range:Grade Distribution:A’s:B’s:C’s:D’s:F’s:

Number Stats Programming Assignment

At the end of one attempt at reading, or a successful read, of a file the user it to be asked if theywould like to evaluate another file of numbers. Use the prompt: Would you like to evaluateanother file? (y/n) If the user answers y, then the program is to accept input for another filename. If the user answers with anything other than y, the program is to exit.

Testing

Once you have written your program you need to test it. A sample file called numbers.txtcontaining a list of integers is provided for testing. It is contained in a file attached to theassignment called numbers.txt.zip. You must unzip the file to get the numbers.txt file. You canalso hand create files or use the random number writer from the programming assignment inmodule 5 to create files for testing. The numbers.txt file, or any other test file containing numbers, needs to be in the same directoryas the python program for it to be found.For the provided numbers.txt file the following are the values your program should generate:

Sum: 56110 Count: 100 Average: 561.1 Maximum: 995 Minimum: 8 Range: 987

For the provided numbersg.txt file the following are the values your program should generate:Enter the name of the file you would like to process: numbersg.txt

File name: numbersg.txtSum: 11393.139999999998Count: 127Average: 89.70976377952753Maximum: 100.0Minimum: 0.0Range: 100.0Grade Distribution:A's: 84B's: 30C's: 9D's: 1F's: 3

Explanation / Answer

def main():

total = 0.0
length = 0.0
average = 0.0
minimum = None
maximum = None
range = None
numAs = 0
numBs = 0
numCs = 0
numDs = 0
numFs = 0

read = True

while read:
try:
  
filename = raw_input('Enter a file name: ')

infile = open(filename, 'r')

for line in infile:
num = float(line.rstrip(" "))
total += num
length = length + 1
if minimum is None or num < minimum:
minimum = num
if maximum is None or num > maximum:
maximum = num
  
  

average = total / length
range = maximum - minimum
if range = 100
for line in infile:
num = float(line.rstrip(" "))
determine_grade(num)


def determine_grade(grade):
if grade >= 90 and <= 100:
numAs++
elif grade >= 80 and <= 89:
numBs++
elif grade >= 70 and <= 79:
numCs++
elif grade >= 60 and <= 69:
numDs++
else:
numFs++


infile.close()

print 'Filename:' , filename
print 'Sum:' , total
print 'Count:', length
print 'Maximum:', maximum
print 'Minimum:', minimum
print 'Range:' , range
print 'Distributions As:', numAs, 'Bs:', numBs, 'Cs:', numCs, 'Ds:', numDs,'Fs:', numFs
print format(average, 'Average:,.2f')


except IOError:
print 'An error occurred trying to read the file.'

except ValueError:
print 'Non-numeric data found in the file'

except:
print('An error has occurred')

again=str(input("Would you like to evaluate another file? (y/n) "))
if again != "y":
read = False