I have to create the following class, but I am unsure of how to set up my constructor.
A GradeRecords object has the following attributes:
term
: a string representing the current semester;grades
: a list object containing tuples, where the first entry of each tuple is a string representing the code of the class, the second entry of each tuple is the grade out of 100, and the third entry is the number of credits for this course.grades
can be initialized as an empty list.num_courses
: an int which contains the number of courses in the record.
This can be initialized as 0.
You are not allowed to add more attributes.
Furthermore, a GradeRecords
object has the following methods:
- an initialization method which takes as input the current term and initializes the three attributes;
- add_course, a method which takes a string representing the course code, an int for the grade out of 100 and the number of credits. The method adds a new tuple to grades.
My code give me the error:
g1 = GradeRecords("Fall 2021")
TypeError: __init__() missing 1 required positional argument: 'new_tuple'
Thank you!
class GradeRecords:
grades = []
num_courses = 0
def __init__(self, term, new_tuple):
self.term = term
#create a list from the input new_tuple
self.grades.append(new_tuple)
GradeRecords.num_courses = len(self.grades)
def add_course(self, course_code, grade_100, num_credits):
new_tuple = (course_code, grade_100, num_credits)
self.grades.append(new_tuple)
return grades
CodePudding user response:
The __init__
of your class has two arguments : term
and new_tuple
.
When you make an instance of your class as g1 = GradeRecords("Fall 2021")
, you give a value for term
(="Fall 2021") but the object is waiting for a new_tuple
as well.
So to make it works, you should give a tuple as a second argument like :
g1 = GradeRecords("Fall 2021", ("Maths", 90, 10))
However, in my opinion, the class
might also be written this way :
class GradeRecords:
def __init__(self, term):
self.grades = []
self.num_courses = 0
self.term = term
def add_course(self, course_code, grade_100, num_credits):
self.grades.append((course_code, grade_100, num_credits))
self.num_courses = len(self.grades)
return self.grades
And we can use it like so :
>>> g1 = GradeRecords('Fall 2021')
>>> g1.add_course("Maths", 90, 10)
[("Maths", 90, 10)]
>>> g1.add_course("Physics", 80, 9)
[('Maths', 90, 10), ('Physics', 80, 9)]
CodePudding user response:
Your __init__
expects 2 arguments while only 1 is passed to it, so you can add a default value to new_tuple
if nothing is passed and can be called as g1 = GradeRecords("Fall 2021")
. If you want it to be initialised to something else then it can be called as g1 = GradeRecords("Fall 2021", ("Course", "<Grade>", "<Credits>"))
class GradeRecords:
grades = []
num_courses = 0
def __init__(self, term, new_tuple=("",0,0)):
self.term = term
#create a list from the input new_tuple
self.grades.append(new_tuple)
self.num_courses = len(self.grades)
def add_course(self, course_code, grade_100, num_credits):
new_tuple = (course_code, grade_100, num_credits)
self.grades.append(new_tuple)
return grades