CS 115 Lab 12, Part C: Write client code for the Student class

[Back to lab instructions]


Instructions

  1. Make a new copy of your lab12b.py file, and name it lab12c.py.
  2. Replace your current version of main with the following:
    def main():
        studentlines = read_input_file("students.txt")
    

    This will require you to define a function called read_input_file that reads the lines of the specified file into a list (one element per line). You can borrow this code from previous labs.

    You should make this a normal function, NOT a method of the Student class!

  3. The list studentlines now has each line of the file as a single string. For example, studentlines[0] will be the string
    Linus   25      12      21		30		29
    However, we need to separate that into a name and a list of integer scores.
  4. Add the following lines of code to the end of main. The next steps will go into more detail about each item.
    # Loop over the list of lines from the file and
    # break each line down into a name and scores
    for line in studentlines:
        # 1. Split line into a list. If list is empty, break out of the loop.
    
        # 2. The first item in the list is the student's name
    
        # 3. Convert the remaining items in the list into integers
    
        # 4. Create a new Student variable with that name and scores
    
        # 5. Call the Student print method to print that student's
        # information
    
  5. Under item 1 in the comments, split line into a list of strings. Save that list as a variable. Break out of the loop if the list is empty. To do so, you can use the keyword break on a line by itself.
  6. Under item 2 in the comments, save the first item in your list into a new variable called name.
  7. Under item 3 in the comments, create a new list for the scores. Then, convert the remaining items into integers and append them to the new list. You may assume that they will be valid integers.
  8. Under item 4, create a new Student variable with that name and those scores. See Part B of your lab if you need a hint.
  9. Finally, under item 5, call the print method to print that student's information. See Part B of your lab if you need a hint.
  10. When you run your code, you should see all of the students from the students.txt file, their scores, and their averages:
    Linus       	25	12	21	30	29	23.4
    Charlie     	12	13	4	9.66667
    Lucy        	30	29	30	29.66667
    Patty       	13	14	15	12	13.5
    Schroeder   	28	21	24.5
    Snoopy      	21	21	21	21.0
    Pigpen      	15	15.0
    Marcie      	30	28	30	29.33333
    Frieda      	15	23	16	18.0
    Spike       	10	8	10	9.33333
    Pooh        	8	9	10	9.0
    Piglet      	24	23	28	25.0
    Christopher 	28	29	30	29.0
    Owl         	30	30	30	30.0
    Rabbit      	29	29	29	29.0
    Eeyore      	12	12	12	12.0
    Kanga       	30	29	30	29.66667
    Roo         	2	1	3	2.0
    Tigger      	0	0	0	0	0	30	5.0
    Heffalump   	21	22	23	22.0
    
  11. Add code to your main function to compute and print the overall average of all the students (that is, the average of all of their averages), rounded to 5 decimal places. You should use the get_average method to get each student's average rather than recomputing it inside main. Your final output should look like this:
    Linus       	25	12	21	30	29	23.4
    Charlie     	12	13	4	9.66667
    Lucy        	30	29	30	29.66667
    Patty       	13	14	15	12	13.5
    Schroeder   	28	21	24.5
    Snoopy      	21	21	21	21.0
    Pigpen      	15	15.0
    Marcie      	30	28	30	29.33333
    Frieda      	15	23	16	18.0
    Spike       	10	8	10	9.33333
    Pooh        	8	9	10	9.0
    Piglet      	24	23	28	25.0
    Christopher 	28	29	30	29.0
    Owl         	30	30	30	30.0
    Rabbit      	29	29	29	29.0
    Eeyore      	12	12	12	12.0
    Kanga       	30	29	30	29.66667
    Roo         	2	1	3	2.0
    Tigger      	0	0	0	0	0	30	5.0
    Heffalump   	21	22	23	22.0
    
    Overall average: 19.30333
    
  12. Call an instructor over to demo your code.
  13. Continue to Part D.