I am having trouble with this program, if someone could please help me and inclu
ID: 3821787 • Letter: I
Question
I am having trouble with this program, if someone could please help me and include the info already given, that would be great. The 3 quiz scores are supposed to be hardcoded in the program.
IN PYTHON PLEASE
Implement a class Student. For the purpose of this exercise, a student has a name and a total quiz score. Supply an appropriate constructor and methods getName(), addQuiz(score), getTotalScore(), and getAverageScore(). To compute the latter, you also need to store the number of quizzes that the student took. Below is the program that runs Student class.
# Create a new student.
s = Student("Bob")
# Add some quiz scores.
s.addQuiz(50)
s.addQuiz(90)
s.addQuiz(75)
# Show that the total and the average are computed correctly.
print(s.getName(), "had a total score of %f." % s.getTotalScore())
print("The average score was %.1f." % s.getAverageScore())
Explanation / Answer
#Implementing Student class in python
class Student:
#__init__() is a special method which will intialize all the variable of Student Class
def __init__(self, name):
self.name = name
self.totalQuizScore = 0
self.totalQuiz = 0
#This method will return the name of student
def getName(self):
return(self.name)
#This method will add up the score and increase the totalQuiz counter which holds total number of quiz student took
def addQuiz(self,score):
self.totalQuizScore=score+self.totalQuizScore
self.totalQuiz=self.totalQuiz+1
#This method will return total quiz score of particular student
def getTotalScore(self):
return(self.totalQuizScore)
#This method will return average quiz score i.e total score divided by number of quiz
def getAverageScore(self):
return(self.totalQuizScore/self.totalQuiz)
# Create a new student.
s = Student("Bob")
# Add some quiz scores.
s.addQuiz(50)
s.addQuiz(90)
s.addQuiz(75)
# Show that the total and the average are computed correctly.
print(s.getName(), "had a total score of %f." % s.getTotalScore())
print("The average score was %.1f." % s.getAverageScore())
#Output :-
Bob had a total score of 215.000000.
The average score was 71.7.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.