In: Computer Science
USING PYTHON 3.7 AND USING def functions.
Write a function called GPA that calculates your grade point average (GPA) on a scale of 0 to 4 where A = 4, B = 3, C = 2, D = 1, and F = 0. Your function should take as input two lists. One list contains the grades received in each course, and the second list contains the corresponding credit hours for each course. The output should be the calculated GPA.
To calculate the grade points for a course, multiply the number of credit hours with the number corresponding to the earned grade. Add all the grade points for all the courses. Then divide the total grade points by the total number of credit hours for all the courses.
For example, the following record would result in a GPA of 2.75:
Program:
def GradePointAverage(Grade,Credit_hours): # called function
totalGrade=0 # initially totalGrade is 0
Total_Credit_hours=0 # initially Total_Credit_hours is 0
for i in range(0,len(Grade)): # Loop to calculate total grade and total credit hours
if Grade[i]=='A':
totalGrade = totalGrade + 4 * Credit_hours[i]
Total_Credit_hours = Total_Credit_hours + Credit_hours[i]
if Grade[i]=='B':
totalGrade = totalGrade + 3 * Credit_hours[i]
Total_Credit_hours = Total_Credit_hours + Credit_hours[i]
if Grade[i]=='C':
totalGrade = totalGrade + 2 * Credit_hours[i]
Total_Credit_hours = Total_Credit_hours + Credit_hours[i]
if Grade[i]=='D':
totalGrade = totalGrade + 1 * Credit_hours[i]
Total_Credit_hours = Total_Credit_hours + Credit_hours[i]
if Grade[i]=='F':
totalGrade = totalGrade + 0 * Credit_hours[i]
Total_Credit_hours = Total_Credit_hours + Credit_hours[i]
GPA = totalGrade / Total_Credit_hours
print("Grade Point Average =",GPA)
Grade = ['A','B','A','C','D'] # Assign grade values to list
Credit_hours = [3,2,4,3,4] # Assign credit points to list
GradePointAverage(Grade,Credit_hours) # calling function
Output: