Sunday, April 1, 2012

Python: Calculate Grade Point Average In a 4.0 Scale

This is a simple program that take a list of grade, and then convert it to a 4.0 GPA scale with 1 decimal point, finally calculate the grade point average.
# Take the list of grade as input, assume list is not empty
def convertGrade(myGrades):
    myResult = [] # List that store the new grade
    for grade in myGrades:
        gpa = (grade / 20) -1
        # Depending on how many deciaml you want
        gpa = round(gpa, 1)
        myResult.append(gpa)
    return myResult

# The list of grades, can be more than 5 if you want to
grades = [88.3, 93.6, 50.2, 70.2, 80.5]
convertedGrades = convertGrade(grades)
print(convertedGrades)

total = 0
# If you want the average of them
for grade in convertedGrades:
    total += grade # add each grade into the total
    
average = total / len(convertedGrades)
print('Average GPA is:', average)
Output:
[3.4, 3.7, 1.5, 2.5, 3.0]
Average GPA is: 2.82

No comments:

Post a Comment