在我的多类示例中如何修复此错误?

问题描述 投票:0回答:1
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

image of the error I got

请详细回答,因为我是初学者。谢谢。

我做错了什么?我怎样才能更有效率? 我怎样才能防止这个错误发生?

python-3.x syntax jupyter
1个回答
0
投票

您的代码存在多个问题:

  1. 您的课程名为

    Students
    但您正在初始化
    Student
    (末尾缺少“s”)

  2. print(course.add_student)
    尝试打印方法
    add_student()
    本身,而不是方法返回的内容。您需要使用参数调用该方法:
    print(course.add_student(s1))

  3. add_student()
    方法返回一个布尔值,因此您的打印语句将打印
    True
    。这在技术上并不是不正确的,但可能不是您想要的。如果您想查看学生是否已成功添加,您可以检查该课程现在是否有两名学生:
    print(len(course.students))

  4. add_student()
    中的 if 语句中,您需要与
    self.max_students
    进行比较,而不是与
    max_students

    进行比较
  5. Course
    内,您没有正确初始化
    self.students
    ,您缺少等号。应该是这样的:
    self.students = []
    。这就是导致您出现错误消息的原因。

这是代码的清理版本:

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
© www.soinside.com 2019 - 2024. All rights reserved.