Skip to content

Instantly share code, notes, and snippets.

@willasaywhat
Created May 10, 2010 18:00
Show Gist options
  • Save willasaywhat/396321 to your computer and use it in GitHub Desktop.
Save willasaywhat/396321 to your computer and use it in GitHub Desktop.
Grade point average from letter grades.
grades_txt = """C+
A-
A-
B+
B-
B
C-
B
B
B
B
A
B+
B+
B
B+
B+
A
C
B
A
B+
C
S
A-
A-
B+
B
A"""
grade_map = {"A":4, "B":3, "C":2, "D":1, "F":0, "S":0}
modifiers = {"+":.25, "-":-0.25}
grades = grades_txt.split("\n")
sum = 0.0
cnt = 0
for g in grades:
print "Grade: %s" % g
# Bypass (un)satisfactory
if g[0] != "S":
grade_int = float(grade_map[g[0]])
if len(g) > 1:
grade_int += modifiers[g[1]]
sum += grade_int
cnt += 1
print "Current grade: %f" % grade_int
print "Total number of grades: %d" % cnt
print "Total sum of grades: %f" % sum
print "Average: %f" % (float(sum) / float(cnt))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment