Skip to content

Commit e717d06

Browse files
committed
Python script to calculate GPA for any semester or overall in college
1 parent 90a36fe commit e717d06

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

GPA Calculator/gpa_calculator.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
class GPACalculator:
2+
def __init__(self, number_of_courses):
3+
self.is_valid_grade = True
4+
self.course_grades = []
5+
self.number_of_courses = number_of_courses
6+
self.if_invalid_grade = ""
7+
8+
def grade_validator(self):
9+
for i in range(self.number_of_courses):
10+
x, y = input(f"Course {i+1} grade: "), int(input(f"Credits for course {i+1}: "))
11+
self.course_grades.append((x.upper(), y))
12+
print()
13+
for x in self.course_grades:
14+
if x[0].upper() not in GRADE_TO_GP:
15+
self.if_invalid_grade = f"{x[0].upper()} is not a valid grade!"
16+
self.is_valid_grade = False
17+
return self.is_valid_grade
18+
return self.is_valid_grade
19+
20+
def overall_gpa_using_previous(self):
21+
if self.grade_validator():
22+
current_gpa = float(input("Current overall GPA: "))
23+
print()
24+
credits_completed_till_now = int(input("Total credits completed till now: "))
25+
print()
26+
current_sem_product = 0
27+
total_credits = 0
28+
for grade, credits in self.course_grades:
29+
current_sem_product += credits*GRADE_TO_GP[grade]
30+
total_credits += credits
31+
return f"Expected Overall GPA after the current semester: {round((current_sem_product + (current_gpa * credits_completed_till_now))/(total_credits + credits_completed_till_now), 2)}"
32+
else: return self.if_invalid_grade
33+
34+
def overall_gpa(self):
35+
if self.grade_validator():
36+
product = 0
37+
total_credits = 0
38+
for i, j in self.course_grades:
39+
product += j*GRADE_TO_GP[i]
40+
total_credits += j
41+
return f"Expected Overall GPA: {round(product/total_credits, 2)}"
42+
else: return self.if_invalid_grade
43+
44+
if __name__ == "__main__":
45+
GRADE_TO_GP = {
46+
"A" : 4,
47+
"A-" : 3.67,
48+
"B+" : 3.33,
49+
"B" : 3,
50+
"B-" : 2.67,
51+
"C+" : 2.33,
52+
"C" : 2,
53+
"F" : 0
54+
}
55+
56+
while True:
57+
choice = input("\n*** GPA Calculator ***\n\n1. Calcualte expected GPA after the current semester\n2. Calculate the GPA using each course's grade\n3. Press 'q' to quit\n\nYour choice number: ")
58+
print()
59+
try:
60+
if choice == "1":
61+
number_of_courses = int(input("Number of courses taken in the current semester: "))
62+
new_gpa = GPACalculator(number_of_courses)
63+
print(new_gpa.overall_gpa_using_previous())
64+
elif choice == "2":
65+
number_of_courses = int(input("Number of courses taken: "))
66+
new_gpa = GPACalculator(number_of_courses)
67+
print(new_gpa.overall_gpa())
68+
elif choice.lower() == "q" or choice == "3":
69+
print("Goodbye!")
70+
x = input("Press enter to exit...\n")
71+
break
72+
else:
73+
print("Enter a valid choice number!")
74+
continue
75+
except Exception as e:
76+
print("\nInvalid input!")

0 commit comments

Comments
 (0)