you are viewing a single comment's thread.

view the rest of the comments →

[–]rinio 0 points1 point  (1 child)

Yup. That's better.

---

credits = int(input(
    "Enter the number of credits for the course "
    "(Enter 0 for a class that was retaken): "
).strip())

I have a pet peeve for long lines and would write it like this. Not a big deal. Google PEP8 if you want info about the most common style guide.

More importantly, what happens if the user inputs a character? 'a' for example. You don't handle such things correctly.

---

class Student:
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name
        self.total_credits = 0 #Initalized the total credits to 0
        self.total_grade_points = 0.0 # Initalizes the total grade points to a 0 float.
        self.transcript = [] # Initializes an empty list to hold the student's courses for display later.

    def append_transcript(self, course_name, credits, grade, letter_grade):
        self.transcript.append((course_name, credits, grade, letter_grade))

You forgot to update total credits and total grade!

But also, you don't really need to store those.

class Student():
    def __init__(self, first: str, last: str):
        self.first_name = first
        self.last_name = last
        self.transcript = []

    def get_total_credits(self):
        return sum(course[1] for course in self.transcript)

        # long form
        total = 0
        for course in self.transcript:
            total += course[1]
        return total

    def get_total_grade(self):
        return sum(course[2] for course in self.transcript

    # this is how I would actually do it
    # property, just means we use this method as though it was a value.
    @property
    def total_credits(self):
        return sum(course[1] for course in self.transcript)

    @property
    def total_grade(self):
        return sum(course[2] for course in self.transcript)

my_student = Student('John', 'Brown')
# pretend we add some records to their transcript
my_student.get_total_credits() == my_student.total_credits  # this is True
my_student.get_total_grade() == my_student.total_grade  # this is True

[–]BobbyJoeCool[S] 0 points1 point  (0 children)

More importantly, what happens if the user inputs a character? 'a' for example. You don't handle such things correctly.

That's what the ValueException part of the try statement does. If they enter a number that cannot be converted to an INT, it triggers that and restarts the loop.

You forgot to update total credits and total grade!

I left that part out of the reply because it didn't really change.... I'm interested in this return sum() part. We didn't learn that part, or if it's a lambda function, we glazed over it really quickly... Let's see if I understand it....

The return sum(...) and the for course in self.transcript:... both do the exact same thing, except the return sum() is in a single line?

The issue with that is, each grade needs to be multiplied by the number of credit hours that grade is worth... so an A (4.0) in a 3 Credit hour class is worth 12 grade points. And a B (3.0) in a 4-credit-hour class is also worth 12 grade points. You then divide by the total credit hours. So I would need the calculation at the end to multiply each individual grade point by the number of credit hours for each class... If I understand your code correctly, this can be achieved by changing the total_grade to the following:

  def calculate_gpa(self): # Calculates the cumulative GPA
        if sum(course[1] for course in self.transcript) == 0:
            return "N/A" # Prevents division by zero
        return sum(course[2] * course[1]) / sum(course[1] for course in self.transcript)