In: Computer Science
Write a program that asks a user how many classes they are taking this term, the name of each class, the credit hours for each class, and their final letter grade for each class, and then calculates their term GPA. Use either a list for each course or a single list-of-lists. The grading system and examples can be found on the TAMU website: http://registrar.tamu.edu/Transcripts-Grades/How-to-Calculate-GPA
I am going to be using python as you have mentioned lists and list of lists.
number_of_classes = int(input("Enter the number of classes you have taken this term : "))
classes = []
print("")
# Loop number_of_classes times to take information about each class.
# Store the classe info in the classes list of list.
for i in range(1,number_of_classes+1):
class_name = input("Enter name of class " + str(i) + " : ")
class_credits = int(input("Enter credit hours for this class : "))
class_gpa = input("Enter the letter grade for this class : ")
class_gpa = class_gpa.upper()
class_info = [class_name, class_credits, class_gpa]
classes.append(class_info)
print("")
# I am going to be using the formula which is given in the website you have provided.
# Intitialize grade points and credit hours to zero
total_grade_points = 0
total_credit_hours = 0
for class_info in classes:
letter_grade = class_info[2]
class_credit = class_info[1]
# Added appropriate amounts to grade points and credit hours as per letter grade.
if(letter_grade == "A"):
total_grade_points += 4*class_credit
total_credit_hours += class_credit
elif(letter_grade == "B"):
total_grade_points += 3*class_credit
total_credit_hours += class_credit
elif(letter_grade == "C"):
total_grade_points += 2*class_credit
total_credit_hours += class_credit
elif(letter_grade == "D"):
total_grade_points += 1*class_credit
total_credit_hours += class_credit
elif(letter_grade == "F"):
total_grade_points += 0*class_credit
total_credit_hours += class_credit
elif(letter_grade == "U"):
total_grade_points += 0*class_credit
total_credit_hours += class_credit
# Grades such as S are ignored from calculations as defined in the formula.
# Total gpa is grade points/ total credits.
gpa = total_grade_points/total_credit_hours
print(("Your final gpa is : %.2f") % (gpa))
The output-
The code has been commented so you can understand the code better.
I would love to resolve any queries in the comments. Please consider dropping an upvote to help out a struggling college kid :)
Happy Coding !!