In Class: Week 8 Thursday:
File I/O, List of lists


Copy over three new files in your week08 directory by running update21:

$ cd
$ cd cs21/class/week08
$ update21 

Topics

In-class work

  1. First, we are going to cover doing input and output using files. Open up filetest.py to see the different ways we can read text from a file
  2. Second, we are going to discuss lists of lists. Open up listoflists.py and follow along to see how we trace this code. Together, we will see how to access one sub-list and then how to index a single element within one of the sub-lists.
  3. Next, we are going to write a program that reads in data from a file and stores it in a list of lists. This program implements processing a gradebook for a class in gradebook.py. It starts by reading in gradebook entries from a file, gradebook.txt. Each entry is of the form:
      Name1  grade1  grade2  grade3  ... gradeN
      Name2  grade1  grade2  grade3  ...  gradeN
    
    We are going to write a function to read in each line into a list of values, the first element will be the name string, the subsequent ones will be the grades stored as int values:
      ["name string", grade1, grade2, ... grade N]
    
    and for each list is creates it will add it to the list of lists to produce a data structure that looks like the following:
                  -------
    gradebook---> | *---|-------> ["name1 string", grade1, grade2, ..., grade N]
                  |-----|
                  | *---|-------> ["name2 string", grade1, grade2, ..., grade N]
                  |-----|
                  | *---|-------> ["name3 string", grade1, grade2, ..., grade N]
                  |-----|
                     .
                     .
                     .
                  |-----|
                  | *---|-------> ["nameM string", grade1, grade2, ..., grade N]
                  -------
    
    
    Then we will write some functions for doing something with this list of lists.

    1. printStudentInfo(student, gradebook): prints out the information about one student given its student number.
    2. printNames(gradebook): prints out the names of all students in the gradbook
    3. printQuizScores(quiznum, gradebook): prints out the student scores for the given quiznumber
    4. printGradeBook(gradebook): prints out the entire grade book contents in tabular format
    5. getAveQuizGrade(quiz_n, gradebook): returns the average quiz grade for a specific quiz
    6. getAveGrade(student, gradebook): returns the average grade for a specific student