Python: Classes that use other classes
Question: So I have 2 files that work together using each other's classes.

I have

    
    
    class Student:
        """A class to model a student with name, id and list of test grades"""
        def __init__(self, name, id):
            """initializes the name and id number; sets list of grades to []"""
            self.s_name = name
            self.ident = id
            self.tests=[]
    
        def getID(self):
            return self.ident
    
        def get_name(self):
            """ returns the student name"""
            return self.s_name
        def addtest(self,t):
            """adds a grade to the list of test grades """
            self.tests.append(t)
        def __str__(self):
            """returns the student name and the current list of grades"""
            return self.s_name + "  " + str(self.tests) + "  " 
    
        def comp_av(self):
            """returns the average of the current set of grades or 'no grades'
            if appropriate"""
            if len(self.tests) > 0:
                sum = 0.0
                for item in self.tests:
                    sum = sum + item
                average = float(sum)/len(self.tests)
                return average
            else:
                return "no grades"
    

Which is completely done. I also have code that is from the teacher's point of
view. The students are not just represented by their names but by an object of
class `Student`. Each Student object has their name and ID number, but also a
list of test scores. Right now Course has only the constructor and the
`__str__` method.

    
    
    from LabStudentClass import *
    
    class Course:
        """ A class to model a course which contains a list of students"""
        def __init__(self,teacher):
            """Sets up a class to hold and update students"""
            self.students = []
            self.teacher = teacher
    
        def __str__(self):
            """ prints the course by listing each student in the class"""
            result = self.teacher+"'s Class\n"
            for s in self.students:
                name = s.get_name()
                result = result + name + '\n'
    
            return result
    
    c = Course("Dr. Bradshaw")
    #print c
    
    def AddStudent(name, id):
        student1 = Student('Mary Comtpon', '3456')
        student2 = Student('Billy Jo', '2345')
        student3 = Student( 'Anne lou', '1090')
    
        print student1
        print student2
        print student3
    

My goal is to create a method `AddStudent`: This method gets two parameters, a
student name and an ID. A new Student object is created and added to the
course.

Add 3 students to your class and print out the class to test it.

However, the students aren't printing and I'm not really sure what the problem
is.


Answer: Add this method to your `Course` class:

    
    
    def addStudent(self, name, id):
        student = new Student(name, id)
        self.students.append(student)
    

Then, replace the function you wrote at the bottom with the following:

    
    
    c.addStudent('Mary Comtpon', '3456')
    c.addStudent('Billy Jo', '2345')
    c.addStudent('Anne lou', '1090')
    
    print c
    

