Here is an example code to calculate GPA using classes in Python:
class Course:
def __init__(self, name, credits, grade):
self.name = name
self.credits = credits
self.grade = grade
class Transcript:
def __init__(self):
self.courses = []
def add_course(self, course):
self.courses.append(course)
def get_gpa(self):
total_quality_points = 0
total_credits = 0
for course in self.courses:
quality_points = self.get_quality_points(course.grade) * course.credits
total_quality_points += quality_points
total_credits += course.credits
return total_quality_points / total_credits
def get_quality_points(self, grade):
if grade == 'A':
return 4.0
elif grade == 'A-':
return 3.7
elif grade == 'B+':
return 3.3
elif grade == 'B':
return 3.0
elif grade == 'B-':
return 2.7
elif grade == 'C+':
return 2.3
elif grade == 'C':
return 2.0
elif grade == 'C-':
return 1.7
elif grade == 'D+':
return 1.3
elif grade == 'D':
return 1.0
elif grade == 'F':
return 0.0
else:
return None
# Example usage
cs101 = Course("Intro to Computer Science", 4, "B+")
math101 = Course("Calculus I", 3, "A")
eng101 = Course("Composition I", 3, "A-")
transcript = Transcript()
transcript.add_course(cs101)
transcript.add_course(math101)
transcript.add_course(eng101)
print("GPA: {:.2f}".format(transcript.get_gpa()))
In this example, we define two classes: Course
and Transcript
. Course
represents a single course with its name, number of credits, and grade received. Transcript
represents a collection of courses and provides a method to calculate the GPA.
The Transcript
class has an add_course
method to add a single course to the collection of courses. The get_gpa
method calculates the total quality points and total credits, then divides the former by the latter to get the GPA. The get_quality_points
method returns the number of quality points associated with a given letter grade.
The example code creates three instances of Course
and adds them to an instance of Transcript
. The get_gpa
method is called on the Transcript
instance and the resulting GPA is printed.
Note that this code only works for numeric grades, such as A, B+, C-, etc. If your school uses a different grading system, you will need to modify the get_quality_points
method accordingly. Also note that this is a very simple implementation that doesn't take into account factors such as repeated courses or weightings for honors/advanced courses.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-07-09 17:47:19 +0000
Seen: 9 times
Last updated: Jul 09 '23
How can I set up Gunicorn with a Django Project?
Looking for a Python Module that finds Tags for a Text describing its Content
Need a Function in Python to remove entries less than 2 digits from an Array
How can I convert a Document in Python?
How can I program a Loop in Python?
How can I enable Python Code Highlighting in Askbot?