< Back

Activity 8

Types

One very common problem I have seen this semester is the loss of the ability to name the type of a particular variable or function. While this is unnecessary as far as the language definition is concerned, we do need to keep up with this information in order to successfully complete programs.


Read the following class definitions, and answer the questions below.

#Author:  Scotty Smith
#Purpose: Represent a Student for a gradebook class

ASSIGNMENTS = "assignment"
QUIZZES     = "quizzes"

class Student:
    
    def __init__(self, input_name):
        self.name = input_name
        self.grades = {}

        self.grades[ASSIGNMENTS] = []
        self.grades[QUIZZES] = []

    def add_grade(self, grade, category):
        self.grades[category].append(grade)

    def compute_assignment_average(self):
        assignment_sum = 0
        for grade in self.grades[ASSIGNMENTS]:
            assignment_sum += grade

        return assignment_sum / len(self.grades[ASSIGNMENTS])

    def compute_quiz_average(self):
        quiz_sum = 0
        for grade in self.grades[QUIZZES]:
            quiz_sum += grade

        return quiz_sum / len(self.grades[QUIZZES])        

    def compute_average(self):
        return (self.compute_assignment_average() +
                self.compute_quiz_average()) / 2

What are the types of the following variables, attributes, and methods?

#Author:  Scotty Smith
#Purpose: A gradebook class keeping track of student grades

import student

class Gradebook:

    def __init__(self):
        self.students = {}

    def add_student(self, name):
        self.students[name] = student.Student(name)

    def add_grade(self, name, category, grade):
        add_found_student = self.find_student(name)
        add_found_student.add_grade(grade, category)

    def get_average(self, name):
        average_found_student = self.find_student(name)
        print(average_found_student.get_average())

    def find_student(self, name):
        found_student = None
        for person in self.students:
            if person.name == name:
                found_student = person

        return found_student

What are the types of the following variables, attributes, and methods?