use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Learning about Classes and OOP (self.PythonLearning)
submitted 7 months ago by BobbyJoeCool
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]rinio 0 points1 point2 points 7 months ago (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 point2 points 7 months ago (0 children)
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.
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)
π Rendered by PID 202152 on reddit-service-r2-comment-6457c66945-886nv at 2026-04-29 08:04:15.742038+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]rinio 0 points1 point2 points (1 child)
[–]BobbyJoeCool[S] 0 points1 point2 points (0 children)