无法运行简单的python程序

时间:2018-04-17 04:22:33

标签: python

我开始学习python。这是一个简单的程序:

class StudentRepo:
    def __init__(self):
        self.student_list = []

    def add(self, student):
        self.student_list.append(student)

    def get_list(self):
        self.student_list




class Student:
    def __init__(self, name, age):
        self.age = age
        self.name = name




from models.student.Student import Student
from services.student.StudentRepo import StudentRepo

s1 = Student("A", 10)
s2 = Student("B", 11)

# What is the issue here ?
StudentRepo.add(s1)
StudentRepo.add(s2)

studentList = StudentRepo.get_list()
for student in studentList:
    print(student.name)

s1 = Student("A", 10)有什么问题?

1 个答案:

答案 0 :(得分:3)

您的代码中有两个错误。首先,这个:

def get_list(self):
    self.student_list

应该是:

def get_list(self):
    return self.student_list

其次,您正在使用您应该使用StudentRepo实例的课程StudentRepo

s1 = Student("A", 10)
s2 = Student("B", 11)

my_roster = StudentRepo()

my_roster.add(s1)
my_roster.add(s2)

studentList = my_roster.get_list()
for student in studentList:
    print(student.name)