Question: class GradesFile: # creating a constructor def _ _ init _ _ ( self , filename ) : self. _ _ filename = filename #
class GradesFile:
# creating a constructor
def initself filename:
self.filename filename
# getter for filename
def getfilenameself:
return self.filename
# setter for filename
def setfilenameself filename:
self.filename filename
# function to calculate average
def calculateAverageself:
# using try and except to handle possible exceptions
try:
# opening the file
infile openselffilename, r
# initializing the total and count
total
count
# using a for loop to read the file
for line in infile:
# converting the line to float and adding it to the total
total evalline
# incrementing the count
count
# calculating the average
average total count
# returning the average
return faverage:f
except IOError:
# printing an error message if the file is not found
printError: File not found'
except ValueError:
printError: Invalid value'
# main function
def main:
# using try and except to handle possible exceptions
try:
# opening the file
file openGradestxtw
# enter grades until the user enters
printEnter grades, enter to quit'
grade intinputEnter grade: # getting the first grade
# using a while loop to get the grades
while grade :
# writing the grade to the file
file.writestrgrade
grade intinputEnter grade:
# closing the file
file rlnse
Modify GradesFile class from Lecture Assignment. Add a new function called "loadGradesData". The header of this function is def loadGradesDataself 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.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
