Average GPA

Write a program that takes a course number, units and grade delimited by spaces, and computes the average GPA when the user doesnt provide input.

import re
from re import findall


GPAG = {"A+":4.3,
        "A":4,
        "A-":3.7,
        "B+":3.3,
        "B":3,
        "B-":2.7,
        "C+":2.3,
        "C":2,
        "C-":1.7,
        "D+":1.3,
        "D":1,
        "D-":0.7,
        "F":0}


all_units = [] 
all_letter_grades = []


def getGrade(user_input):
    all_data = user_input.split()
    all_units.append(int(all_data[1]))
    all_letter_grades.append(all_data[2])


def calcGPA():
    total_qp = 0
    total_hours = 0

    for i in range(len(all_units)):
        letter = all_letter_grades[i]
        hrs = all_units[i]
        total_hours += hrs
        total_qp += GPAG[letter] * hrs

    return round(total_qp/total_hours, 2)


while True:
    user_input= input('Enter course number, units, and grade, separated by spaces\n')
    if not user_input:
        print("The GPA is:", calcGPA())
        break
    else:
        getGrade(user_input)

Now improve your program to take in a file of student summary course, and output to a file that summaries the average GPA per student.

File in:

Last updated

Was this helpful?