Consider the following class definition: class Student(object): Represent a student in a fictional course CS 99 Argument: name (string): the student name Attributes: name
Consider the following class definition:
class Student(object): """ Represent a student in a fictional course CS 99 Argument: name (string): the student name Attributes: name (string): the student name grades (list of floats): a list containing the student's grades, if any """ def __init__(self, name): self.name = name self.grades = [] def add_grade(self, grade): """ Add the given grade earned by the student. Parameter: grade (float) - the grade to be added Returns: None """ self.grades.append(grade)
Write a property, average, for the Student class that returns the average grade of the student assuming all the grades have equal weight (The average is the sum of all the grades divided by the total number of grades).
To get any credit on this question, you should not modify __init__ or add_grade and you should not store the average as an instance variable.
If the student does not have any grades, the average should be 0.
Once you implement the average property, you should be able to test it as follows:
alex = Student('Alex') print(alex.average) # should print 0 zoe = Student('Zoe') zoe.add_grade(99.5) zoe.add_grade(89.5) zoe.add_grade(80) zoe.add_grade(70) print(zoe.average) # should print 84.75
only include the property definition in your answer. Make sure you use Python built-in capabilities.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started