Question: Can you help me , please, modify my code? # creating a class called GradesFile class GradesFile: # creating a constructor def _ _ init
Can you help me please, modify my code?
# creating a class called GradesFile
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.close
# creating an object of the GradesFile class
gradesFile GradesFileGradestxt
average gradesFile.calculateAverage
printAverage: average
# handling possible exceptions
except ValueError:
# printing an error message if the user enters a noninteger value
printError: Invalid value.
except:
printError: An error occurred.
# calling the main function
main
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
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
