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

grades.txt contains an unknown number of grades (one per line). Your program wil

ID: 3809649 • Letter: G

Question

grades.txt contains an unknown number of grades (one per line). Your program will create an output file named grades_out.txt. The output file will contain a letter grade for each numeric grade in the input file, followed by a histogram of grades using asterisks. Be sure to catch all exceptions (file does not exist, invalid data) and display an appropriate (different) message for each exception type. If an exception occurs, report it and end your program gracefully.

For example, if grades.txt contains:

56.5

78

92

77.5

88

85

68

79

92

Then grades_out.txt would contain:

F

C

A

C

B

B

D

C

A

A: **

B: **

C: ***

D: *

F: *

Please answer in python! Thank you!

Explanation / Answer

grades = {'A':0, 'B':0, 'C':0, 'D':0, 'E':0, 'F':0}

def isNumber(line):
line = line.strip().replace('.','',1)
return line.isdigit()
  
def getGrade( score ):
if(score >= 90):
return 'A'
elif (score >= 80):
return 'B'
elif(score >= 70):
return 'C'
elif(score >= 60):
return 'D'
else:
return 'F'

try:
output = open('myfile', 'w')
with open('grades.txt') as inputFile:
for line in inputFile:
line = line.rstrip(' ')
if isNumber(line):
if float(line) > 0 :
grade = getGrade(float(line))
output.write(grade + ' ')
grades[grade] = grades[grade] + 1
else :
print('grade can't be negative')
else :
print(line + "Not a valid Grade")
  
for grade in sorted(grades):
if(grades[grade] != 0) :
output.write(grade + ': ' + ("*" * grades[grade]) + " ")
  
output.close()
  
except IOError:
print "Could not read file:", fname
sys.exit()
  

PLease keep the indentation as shown above.

input file:
56.5
78
92
77.5
88
85
68
79
92



Output:
F
C
A
C
B
B
D
C
A
A: **
B: **
C: ***
D: *
F: *