# 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_data = user_input.split()
all_units.append(int(all_data[1]))

def calcGPA():
total_qp = 0
total_hours = 0

for i in range(len(all_units)):
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:

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:

``````eric, davidson
ecs10 4 B-
ecs30 3 C+

dan, mak
ecs50 4 A
ecs60 4 A
sas30 4 A``````
``````eric, davidson            2.52
dan, mak                  4.00``````
``````#12.8 GPA reports using files

GPAG = {'A':4.0,
'A-':3.67,
'B+':3.33,
'B':3.0,
'B-':2.67,
'C+':2.33,
'C':2.0,
'C-':1.67,
'D+':1.33,
'D':1.0,
'D-':0.67,
'B+':0.33,
'B':0.0
}

all_units = []
current_name = "";

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

return round(total_qp/total_hours, 2)

def contains_comma(line):
for char in line:
if (char == ','):
return True

def contains_number(line):
for char in line:
if (char.isdigit()):
return True

file_out = open("GPA_output.txt", "w")
file_in=input('Enter the file name: ')
with open(file_in, 'r') as file:
for line in lines:

if (contains_comma(line)):
current_name = line.rstrip();
continue

elif(contains_number(line)):
continue;

elif(line == "\n"):
file_out.write('%-26s%.2f\n' % (current_name, calcGPA()));
all_units = []