his represents an entire grouping of Grade values as a list

his represents an entire grouping of Grade values as a list (named grades).
We can then dig through this list for interesting things and calculations by
calling the methods we\'re going to implement.

class GradeBook: Define the GradeBook class.

- def __init__(self): GradeBook constructor. Create the only instance variable,
   a list named grades, and initialize it to an empty list. This means that we
   can only create an empty GradeBook and then add items to it later on.
  
- def __str__(self): returns a human-centric string representation. We\'ll
   choose a multi-line representation (slightly unusual) that contains
   \"GradeBook:\" on the first line, and then each successive line is a tab, the
   str() representation of the next Grade in self.grades, and then a newline
   each time. This means that the last character of the string is guaranteed to
   be a newline (regardless of if we have zero or many Grade values).
  
- def __repr__(self): We will be lazy and tell this to just represent the
   exact same thing as __str__. But don\'t cut-paste the code! Use this as the
   entire body of this function:   return str(self)
  
- def add_grade(self, grade): append the argument Grade to the end of
   self.grades

- def average_by_kind(self, kind): Look through all stored Grade objects in
   self.grades. All those that are the same kind as the kind parameter should be
   averaged together (sum their percents and divide by the number of things of
   that kind). If none of that kind exist, return None.
  
- def get_all_of(self, kind): create and return a list of references to each
   Grade object in this GradeBook that is of that kind.
  
- def get_by_name(self, name): search through self.grades in order and return
   the first Grade by the given name. If no such Grade value can be found (say,
   name==\"whatever\"), then raise a GradingError with the message \"no Grade
   found named \'whatever\'\". (You can skip this exception-raising part and come
   back to it).
\"\"\"
class GradeBook:
pass # replace this with all the required method definitions.

Solution

class GradeBook:
    def __init__(self):
        self.grades = []

    def __str__(self):
        s = \"\"
        s += \"GradeBook:\ \"
        for name in self.grades:
            for grade in name:
                s += str(grade)+\"\\t\"
            s += \"\ \"
        return s

    def __repr__(self):
        return str(self)


    def add_grade(self, grade):
        self.grades.append(grade)

    def average_by_kind(self, kind):
        sum = 0
        n = 0
        for name in self.grades:
            for grade in name:
                if grade.kind == kind:
                    sum += grade
                    n += 1
        if not n==0:
            return sum/n
        else:
            return None

    def get_all_of(self, kind):
        all_of = []
        for name in self.grades:
            for grade in name:
                if grade.kind == kind:
                    all_of.append(grade)

    def get_by_name(self, name):
        for nameinlist in self.grades:
            if nameinlist == name:
                return namelist[0]
        return None

his represents an entire grouping of Grade values as a list (named grades). We can then dig through this list for interesting things and calculations by calling
his represents an entire grouping of Grade values as a list (named grades). We can then dig through this list for interesting things and calculations by calling

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site