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 __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.close()
# creating an object of the GradesFile class
gradesFile = GradesFile('Grades.txt')
average = gradesFile.calculateAverage()
print('Average:', average)
# handling possible exceptions
except ValueError:
# printing an error message if the user enters a non-integer value
print('Error: Invalid value.')
except:
print('Error: An error occurred.')
# calling the main function
main()```
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()
```
Can you help me , please, modify my code? #

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!