Transcript Document

The fifth
programming
assignment
J.-F. Pâris
[email protected]
Fall 2014
What we want to do

Read a file containing the list of courses a student
has taken

Print out his or her cumulative GPA
How we would do it manually (I)

Read the file:
 SOCI 3330 Lady Gaga and the Sociology of Fame A PHIL 4320 Philosophy and Star Trek A
 PHYS 4380 Advanced Quantum Physics W
 LING 3370 Invented Languages: Klingon and Beyond B MEDI 4330 Zombies in Popular Media B+
 CRAF 2330 Underwater Basket Weaving B+
How we would do it manually (II)

Find the letter grades
 SOCI 3330 Lady Gaga and the Sociology of Fame A PHIL 4320 Philosophy and Star Trek A
 PHYS 4380 Advanced Quantum Physics W
 LING 3370 Invented Languages: Klingon and Beyond B MEDI 4330 Zombies in Popular Media B+
 CRAF 2330 Underwater Basket Weaving B+

Skip the lines with a W
How we would do it manually (III)

Convert to numerical grades
 A- becomes 3.67
 A becomes 4
 W does not count
 B- becomes 2.67
 B+ becomes 3.33
 B+ becomes 3.33
How we would do it manually (IV)


Add the grades
Count all lines with a grade
 A- becomes 3.67
 A becomes 4
 W does not count
 B- becomes 2.67
 B+ becomes 3.33
 B+ becomes 3.33

Five lines and a total of 17. GPA is 17/5 = 3.4
How your program will do it (I)



Initialize line counter and total of grades
Set up the dictionary dict
 dict = {'A' : 4.00, …}
Open the input file
How your program will do it (II)

For each line in your file:
 Split the line into a list linelist
 grade = linelist[-1]
# helps readability
 if grade[-1] == '\n':
 grade = grade[:-1]
 if grade == 'W' :
 continue # instead of break
 count += 1
 total += dict[grade]
How your program will do it (III)


Print the number of course completed
Print the GPA: total/count
Another main loop

For each line in your file:
 Split the line into a list linelist
 grade = linelist[-1]
# helps readability
 if grade[-1] == '\n':
 grade = grade[:-1]
 if grade != 'W' :
 count += 1
 total += dict[grade]
File handling

Opening a file
 handle = open("record.txt ")

Processing the contents of an opened file
one line at a time
 for line in handle :
 …

Next step will be splitting the line