In: Computer Science
Write a Python program that creates a class which represents a Student Grade. The class includes a function that reads a text file called Course_Score.txt. A sample of the file is provided below. Each row in the file corresponds to the Last Name, First Name, ClassWork score (100), Mid-Term score (100), and Final-Exam score (100). Also include the the following functions to process the content read from the file. a. getData(): This method reads the data from a file and stores the data as a list. b. grade(): This method calculates total score, percentage and grade for each student. A grade (above 90%), B grade (above 80%), C grade (above 60%), D grade (above 50%), and F grade (below 50%) c. highestTotal() and lowestTotal(): These methods calculate highest and lowest total scores in class.
Sample input file: Course_Score.txt
Naji Hasan 90 85 87
Lisa Smith 80 67 70
Andy Malik 75 80 52
Ravi Gupta 90 95 98
Dave Blair 50 61 70
Sara Clark 70 65 81
Sami Moosa 55 50 71
Imed Radhi 90 83 89
Kira Sunny 65 70 69
Hind Ahmed 70 81 88
SOLUTION-
I have solve the problem in python code with comments and
screenshot for easy understanding :)
CODE-
main.py
#python code
#class
class studentGrades:
# to read the file
def __init__(self):
self.file=open('Course_Score.txt','r')
self.data=[]
self.grades=[]
self.totals=[]
#gets data from file
def getData(self):
for line in
self.file.readlines():
self.data.append(line.rstrip().split())
self.file.close()
return (self.data)
def grade(self):
#loop to gives the
grades to students
for d in
self.data:
self.Fname,self.Lname,*self.scores = d
self.totalScore = int(self.scores[0])+\
int(self.scores[1])+int(self.scores[2])
self.totals.append(self.totalScore)
self.percent = round(self.totalScore/3)
if self.percent>=90:
self.grade='A'
elif self.percent>=80:
self.grade='B'
elif self.percent>=60:
self.grade='C'
elif self.percent>=50:
self.grade='D'
elif self.percent<=50:
self.grade='F'
self.grades.append((self.Fname+'
'+self.Lname,self.totalScore,self.percent,self.grade))
#prints the name , total , nPercentage ,grades
print('Name: {0}\nTotal Score: {1}\nPercentage: {2}\nGrade:
{3}\n'\
.format(self.Fname+'
'+self.Lname,self.totalScore,self.percent,self.grade))
#for highest
def highestTotal(self):
return
max(self.totals)
#for lowest
def lowestTotal(self):
return
min(self.totals)
#initalize
Grades = studentGrades()
data = Grades.getData()
#print data
print(data)
print()
#call functions
Grades.grade()
highTotal = Grades.highestTotal()
lowTotal = Grades.lowestTotal()
#print high and low
print('The highest total score is {}'.format(highTotal))
print('The lowest total score is {}'.format(lowTotal))
Course_Score.txt
Naji Hasan 90 85 87
Lisa Smith 80 67 70
Andy Malik 75 80 52
Ravi Gupta 90 95 98
Dave Blair 50 61 70
Sara Clark 70 65 81
Sami Moosa 55 50 71
Imed Radhi 90 83 89
Kira Sunny 65 70 69
Hind Ahmed 70 81 88
SCREENSHOT-
Course_Score.txt
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I
WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------