Question
Hello, When I try to print student which is a list, I am keep having maximum recursion depth exceeded while getting the str of an
Hello,
When I try to print student which is a list, I am keep having "maximum recursion depth exceeded while getting the str of an object" error at my def __repr__ function.
Can someone please help me what is wrong w my code?
Thank you so much!
class Student(object): def __init__(self, id, firstName, lastName, courses = None): self.id = id self.firstName = firstName self.lastName = lastName self.courses = dict() if courses == None else courses
def gpa(self): if len(self.courses.key()) == 0: return 0 else: for course in self.courses: sum = sum(self.courses.value()) return (sum/(len(self.courses.key())))
def addCourse(self, course, score): assert type(score) is float and score >= 0 or score <5, "Score must be numbers between 0 to 4" self.courses[course] = score
def addCourses(self, courses): assert type(courses) is dict, "Courses must be dictionary" self.courses.update(courses)
def __str__(self): return (f"{self.id:<10} {self.lastName:<16} {self.firstName:<13} {self.gpa:>5} {self.courses:<42}")
def __repr__(self): return (f"{self.id}{','}{self.lastName}{','}{self.firstName}{','}{self.gpa}{','}{self.courses}")
@classmethod def header(cls): return (f"{'ID':<10}{'Last Name':<16}{'First Name':<18}{'GPA':<4}{'Courses':<42} {'='*70}")
student = []
s1 = Student(123456, 'Johnnie', 'Smith') s1.addCourses({'CSE-101': 3.50}) s1.addCourses({'CSE-102': 3.00}) s1.addCourses({'CSE-201': 4.00}) s1.addCourses({'CSE-220': 3.75}) s1.addCourses({'CSE-325': 4.00})
student.append(s1)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started