CS 115 Lab 12, Part B: Define a Student class

[Back to lab instructions]


Instructions

  1. Create and open a new Python source code file named lab12b.py:
    """
    Program: Lab 12
    Author: Your name
    This program will eventually read in a list of students and grades from a text
     file, calculate the students' averages, and print the list of students.
    """
    
    
    class Student:
        """ A class that holds the data for an individual student """
        def __init__(self, name, scores):
            self.name = ''
            self.scores = []
    
        def get_average(self):
            """ Return the average grade """
            pass
    
        def print(self):
            """ Print the student info in the following format:
               Name (12 characters), grades(separated by tabs), average (formatted
               to 2 decimals """
    
            # Right now, just prints the student name padded out to 12 characters
            string_to_print = self.name.ljust(12)
    
            print(string_to_print)
    
    
    # A tester program
    def main():
        # Try to create and print a student with grades of 8, 9, and 10
        test_student = Student('Test', [8, 9, 10])
        test_student.print()
    
    
    main()
    
  2. Read over the program and run it in the the online Python 3 tutor. Pay attention to how the test_student variable is stored. Then answer Question 1 in your writeup.
  3. Right now, __init__ is initializing the self.name instance variable to the empty string. That means that when we call the print method, we will print an empty string.

    Modify init so that it sets self.name to the name that was passed to it. When you rerun or re-visualize your program, you should see the following output:
    Test
  4. Now that the student's name is being printed correctly, let's try to print the student's scores. Add the following lines to the print method, just before the print(string_to_print):
    # Convert list of integers to strings for printing purposes
    # There are shorter ways to do this, but this is the clearest.
    for score in self.scores:
        string_to_print += '\t' + str(score)
    
    This will add the list of scores to the string. The scores will be separated by tabs.
  5. Run your program again. You should still only see
    Test
    Answer Question 2 in your writeup.
  6. In the __init__ function, modify the line that initializes self.scores so that self.scores uses the value that was passed to it. Rerun your program. Now you should see:
    Test        	8	9	10
  7. Finally, you should print the average. Add the following line to the print method, just before the print(string_to_print):
    string_to_print += '\t' + str(self.get_average())
    When you run your program, you should see:
    Test        	8	9	10	None
  8. You need to actually define the method get_average for your program to print a value other than None. This method should do the following: It's OK if this function crashes when the list contains non-numeric scores.
  9. When you run your program, you should see the output below:
    Test        	8	9	10	9.0
  10. Experiment with the contents of main to be sure that your program works for different student names and scores.
  11. When your code is correct, continue to Part C, where we will read in the student data from a file.