Question: class GradesFile: # creating a constructor def _ _ init _ _ ( self , filename ) : self. _ _ filename = filename #

class GradesFile:
# creating a constructor
def __init__(self, filename):
self.__filename = filename
# getter for filename
def get_filename(self):
return self.__filename
# setter for filename
def set_filename(self, filename):
self.__filename = filename
# function to calculate average
def calculateAverage(self):
# using try and except to handle possible exceptions
try:
# opening the file
infile = open(self.__filename, 'r')
# initializing the total and count
total =0
count =0
# using a for loop to read the file
for line in infile:
# converting the line to float and adding it to the total
total += eval(line)
# incrementing the count
count +=1
# calculating the average
average = total / count
# returning the average
return f'{average:.2f}'
except IOError:
# printing an error message if the file is not found
print('Error: File not found')
except ValueError:
print('Error: Invalid value')
# main function
def main():
# using try and except to handle possible exceptions
try:
# opening the file
file = open('Grades.txt','w')
# enter grades until the user enters -1
print('Enter grades, enter -1 to quit')
grade = int(input('Enter grade: ')) # getting the first grade
# using a while loop to get the grades
while grade !=-1:
# writing the grade to the file
file.write(str(grade)+'
')
grade = int(input('Enter grade: '))
# closing the file
file rlnse()
``` Modify GradesFile class from Lecture 8 Assignment. Add a new function called "loadGradesData". The header of this function is def loadGradesData(self). This function uses a loop to load grades in the file (defined by _filename attribute of the class) to a list and return the list to the caller. Modify init function to add one more data attribute "__gradesList". Inside function, call "loadGradesData" function and assign the returned value to "_gradesList". Modify calculateAverage function to use the data attribute "__gradesList" to calculate average grades. Main function remains unchanged.
class GradesFile: # creating a constructor def _

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!