Gpa Python Assignment
I have an Python assignment at hand, and I was wondering if you guys can point me in the right direction. I have done the assignments so far up to date, but this one seems to be a
Solution 1:
Edit: Much nicer version (also fixed req. to force float division in 2.7.x etc..):
from re import findall
GPAG = {"A":4,"B":3,"C":2,"D":1,"E":0}
defcalcGPA(ResultStr):
courses = [ dict(zip(["grade","units"],score)) for score in findall(r'COURSE\s(\w)\s(\d+)',ResultStr) ]
print(courses)
unitCount = sum([int(course["units"]) for course in courses])
returnsum([GPAG[course["grade"]]*int(course["units"]) for course in courses] )/float(unitCount)
fname = raw_input("Enter the name of the file of grades: ")
withopen(fname+".txt") as f:
print("The GPA is: %.3f" % calcGPA(f.read()))
With file:
COURSE A4
COURSE B3
COURSE C 5
COURSE D 5
Produces:
>>>
Enter the name of the file of grades: GPA
The GPA is: 2.353
Post a Comment for "Gpa Python Assignment"