Pythong 3, Do not use Loops. Part III: Recursive GPA Calculator (20 points) a si
ID: 3597957 • Letter: P
Question
Pythong 3, Do not use Loops.
Part III: Recursive GPA Calculator (20 points) a single argument: grades: a list containing alternating letter grades and credits for a group of courses taken by a student (see below for further explanation). Only the grades A, B, C, D and F are possible. A list of grades like ['A', 3, 'B', 4, 'D', 3] indicates that the student took three courses with the respective credit values (3, 4 and 3). The function computes the weighted GPA and returns it. Note that letter grades can be provided in uppercase or lowercase. Note: Your code should be able to handle invalid grades by returning None. Invalid grades include: 1. A negative or zero credit value. 2. A letter grade that is not one of A/a/B/b/C/c/D/d/F/f. Hint: Consider using helper functions to calculate (1) the total number of credits and (2) the GPA. Pseudocode is given for both below: Pseudocode for a helper function that calculates the credit total: def credit_sum_helper (grades): if the length of grades is zero then return 0.0 otherwise let x -the letter grade of the first course in the list let y = the # of credits of the first course in the listExplanation / Answer
def credit_sum_helper(grades):
if grades == None or len(grades) == 0:
return 0.0
x = grades[0]
y = grades[1]
if y < 0 or not x in "AaBbCcDdFf":
return None
z = credit_sum_helper(grades[2:])
if z == None:
return None
return y + z
def get_points_for_grade(g):
if g == 'A' or g == 'a':
return 4.0
elif g == 'B' or g == 'b':
return 3.0
elif g == 'C' or g == 'c':
return 2.0
elif g == 'D' or g == 'd':
return 1.0
return 0.0
def grade_points_helper(grades):
if grades == None or len(grades) == 0:
return 0.0
x = grades[0]
y = grades[1]
if y < 0 or not x in "AaBbCcDdFf":
return None
g = get_points_for_grade(x)
r = grade_points_helper(grades[2:])
if r == None:
return None
return y*g + r
def gpa_calculator(grades):
total_grade_points = grade_points_helper(grades)
if total_grade_points == None:
return None
credit_sum = credit_sum_helper(grades)
if credit_sum == None or credit_sum == 0:
return None
return total_grade_points/credit_sum
print(gpa_calculator(['A', 4, 'B', 4, 'A', 4]))
print(gpa_calculator(['A', 4, 'O', 4, 'C', 1, 'F', 2]))
# copy pastable code link: https://paste.ee/p/qJZ6g
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.