Overview
Objectives for today's lab:
- Become familiar with File I/O
- Learn to parse multiple kinds of information from a line of text
Grade Report
- You will be writing a program that computes weighted final grades
for 201 students and the class average.
- For this task, you'll be provided a file, grades.txt, containing the
students' names, and each assignment's weight and grade.
- Your program will produce a file named grade_report.txt that contains
the name of each student, their course score and their letter grade
for the course and finally the class average.
- The student grade file, grades.txt, has the following content:
Alice 0.03 74 0.03 79 0.03 72 0.03 69 0.03 81 0.03 75 0.03 73 0.03 70 0.08 75 0.08 73 0.25 82 0.25 71 0.10 28
Bob 0.03 84 0.03 79 0.03 87 0.03 90 0.03 85 0.03 82 0.03 86 0.03 83 0.08 87 0.08 89 0.25 82 0.25 84 0.10 30
Charlie 0.03 66 0.03 63 0.03 89 0.03 65 0.03 57 0.03 70 0.03 64 0.03 67 0.08 65 0.08 62 0.25 70 0.25 61 0.10 27
Lisa 0.03 91 0.03 99 0.03 89 0.03 90 0.03 100 0.03 100 0.03 98 0.03 95 0.08 85 0.08 100 0.25 94 0.25 92 0.10 34
Getting Started
- Since you will be reusing the code you wrote last week for the
functions lab, you'll want to make a copy of your lab5.py file and
just edit it. Call this copy lab6.py and put it in your lab6
directory.
- To get a copy of the grades.txt file, use the following command:
cp /afs/umbc.edu/users/b/o/bogar/pub/grades.txt .
You'll be reusing all of the functions you wrote last week for
this program. Only main() requires modification to handle:
- getting information on multiple students.
So you will still have:
- main()
- findCourseScore(courseDataList) - This function takes the name of a
student, reads their grades, calculates and returns their course
score.
- findLabScore(labScore) - This function takes the raw lab
score as shown in the file in the range of 0 - 30 and calculates
a final lab score in the range of 0 - 100. See the more detailed
instructions later in the lab.
- findLetterGrade(courseScore) - this function takes a numeric grade,
and returns a string, either 'A', 'B', 'C', 'D', or 'F'
Techniques
- Recall that in order to iterate through each line in a file we use
the following code :
infile = open("infilename.txt", "r")
for line in infile:
#do something with the variable line
infile.close()
- To write a string to a file we use the following code :
outfile = open("outfilename.txt", "w")
outfile.write("Some string")
outfile.close()
- In this case, when we read a line from the grades file, there
are multiple pieces of data on one line.
- To be usable, we'll need to separate the data. We can use the string
split() method.
- The split() method will return a list of strings that
represent each piece of text that is separated by blank space in the
original string.
- For example observe the following code :
>>> aLine = 'Lisa 0.03 91'
>>> aLine.split()
['Lisa', '0.03', '91']
If we store the result of the string split in a variable,
we can easily delete the student's name from the front of the list
leaving a list of weights and scores.
This still isn't quite what we need for our findCourseScore function,
although we're close. We'll need to change the list of strings into
a list of floats before we can pass it to our function.
grade_report.txt
Here is what the grade_report.txt file should look like:
Alice: 77.21 - C
Bob: 85.86 - B
Charlie: 68.14 - D
Lisa: 95.49 - A
Class average: 81.68
Bonus Task
Alice: 77.21 - C
Bob: 85.86 - B
Charlie: 68.14 - D
Lisa: 95.49 - A
Class average: 81.68
A: 1
B: 1
C: 1
D: 1
F: 0
Challenge Problem
- Write a challenge6.py file that will append the grades.txt file with
a new student's name and grades.
- You should do error checking on the input to insure that:
- The weights should add up to 1.00
- Grades should be in the following ranges:
- Assignment grades (HWs & Projs): 0 - 115
- Exam grades: 0 - 105
- Lab grade: 0 - 34
-