Issue
class Students: #class is called Students
def__init__(self, name, age, grade): #constructor for self, name, and age, for Students
self.name = name
self.age = age
self.grade = grade
def get_grade(self):
return self.grade
class Course: #this class is called Course
def__init__(self, name, max_students): #constructor to get name of Course and max number of students in Course
self.name = name
self.max_students = max_students
self.students[]
def add_student(self, student): #code to add students into course
if len(self.students) < max_students:
self.students.append(student) #if theres less students than the max number of students for Course, then append into course until it reaches max number of students
return True
return False
def get_average_grade(self): #code to find the average
pass
s1 = Student("Sam", 19, 78) #s1 is now accessing the Student class. Name, age and grade
s2 = Student("Bam", 19, 58)
s3 = Student("Pam", 19, 48)
course = Course("English", 2) #course is now accessing the Course class.Name of course and max number of students for course.
course.add_student(s1)
course.add_student(s2) #using add_student attribute in Course class to add students into course
print(course.add_student) #printing the result
Please be detailed with your answers, since I am a beginner. Thanks.
What have I done wrong? How could i have been more efficient? And how can I prevent this error from happening?
Solution
There are multiple issues with your code:
Your class is called
Students
but you're initializingStudent
(missing the "s" at the end)print(course.add_student)
tries to print the methodadd_student()
itself instead of what the method returns. You need to call the method with an argument:print(course.add_student(s1))
The
add_student()
method returns a boolean, so your print statement will printTrue
. This is not technically incorrect, but maybe not what you want. if you want to see if the students have been added successfully you can instead for example check that the course now has two students:print(len(course.students))
Within your if statement in
add_student()
you need to compare toself.max_students
, not tomax_students
Within
Course
you are not initializingself.students
correctly, you are missing the equal sign. It should be like this:self.students = []
. This is what was causing your error message.
Here's a cleaned up version of your code:
class Students:
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
def get_grade(self):
return self.grade
class Course:
def __init__(self, name, max_students):
self.name = name
self.max_students = max_students
self.students = []
def add_student(self, student):
if len(self.students) < self.max_students:
self.students.append(student)
return True
return False
def get_average_grade(self):
pass
s1 = Students("Sam", 19, 78)
s2 = Students("Bam", 19, 58)
s3 = Students("Pam", 19, 48)
course = Course("English", 2)
print(len(course.students)) # You haven't added students yet, so this will print 0
result1 = course.add_student(s1)
result2 = course.add_student(s2)
result3 = course.add_student(s3) # This will be false and no student will be added since the max_student limit is exceeded
print(result1)
print(result2)
print(result3)
print(len(course.students)) # Now you have two students in your course
Answered By - Ada
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.