gpt4 book ai didi

python - 无法运行简单的python程序

转载 作者:行者123 更新时间:2023-11-30 22:16:44 27 4
gpt4 key购买 nike

我开始学习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) 有什么问题吗?

最佳答案

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

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)

关于python - 无法运行简单的python程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49869895/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com