Python在函数中使用类并附加到列表

Python在函数中使用类并附加到列表,python,list,function,class,Python,List,Function,Class,我对Python和类比较陌生 我想创建一个Person对象,该对象具有诸如名称、年龄等特征。然后,我想将许多不同的Person对象附加到一个列表中。我在函数中引用类时遇到问题,因为它正在寻找参数,但在我放入参数时似乎不起作用第13行 班级人员: 定义初始(自我、姓名、年龄、计算、数学、英语): self.name=名称 self.age=年龄 自我计算 自我数学 self.english=英语 def getPeople(num): 人=[] 对于范围内的i(num): newPerson=Pe

我对Python和类比较陌生

我想创建一个Person对象,该对象具有诸如
名称
年龄
等特征。然后,我想将许多不同的Person对象附加到一个列表中。我在函数中引用类时遇到问题,因为它正在寻找参数,但在我放入参数时似乎不起作用<代码>第13行

班级人员:
定义初始(自我、姓名、年龄、计算、数学、英语):
self.name=名称
self.age=年龄
自我计算
自我数学
self.english=英语
def getPeople(num):
人=[]
对于范围内的i(num):
newPerson=Person()
newPerson.name=输入(“人员姓名是什么?:”)
newPerson.age=输入(“人员年龄是多少?”)
newPerson.computing=输入(“persons计算分数是多少?”)
newPerson.math=输入(“persons的数学分数是多少?”)
newPerson.english=输入(“persons英语分数是多少?”)
people.append(newPerson)
返回人
getPeople(5)
回溯(最近一次呼叫最后一次):
文件“C:\Users\records.py”,第24行,在
getPeople(5)
getPeople中第13行的文件“C:\Users\srecords.py”
newPerson=Person()
TypeError:\uuuu init\uuuuu()缺少5个必需的位置参数:“名称”、“年龄”、“计算”、“数学”和“英语”
[在0.2秒内完成,退出代码为1]

您需要首先收集参数,然后将它们传递给
人员

def getPeople(num): 
    people = []
    for i in range(num):
        name = input("What is the persons name?: ")
        age = input("What is the persons age?: ")
        computing = input("What is the persons Computing score?: ")
        maths = input("What is the persons Maths score?: ")
        english = input("What is the persons English score?: ")

        people.append(Person(name, age, computing, maths, english))

    return people

people = getPeople(5)
注意,这里有一个使用类方法的好例子

class Person:
    def __init__(self, name, age, computing, maths, english):
        self.name = name
        self.age = age
        self.computing = computing      
        self.maths = maths
        self.english = english

    @classmethod
    def from_input(cls):
        name = input("What is the persons name?: ")
        age = input("What is the persons age?: ")
        computing = input("What is the persons Computing score?: ")
        maths = input("What is the persons Maths score?: ")
        english = input("What is the persons English score?: ")
    
        return cls(name, age, computing, maths, english)


def getPeople(num): 
    return [Person.from_input() for _ in range(num)]

您为该类添加了一个init方法,因此在调用
Person()
类时,需要将所有这些变量作为参数传递。例如:

name = input()
age = input()
....
new_person = Person(name, age, ...)
people.append(new_person)

\uuuu init\uuuu()
方法的参数指定调用
Person()
时必须提供的参数(自动传递的
self
除外)。因此,您需要在那里传递所有属性值,而不是在创建person之后分配它们

def getPeople(num): 
    people = []
    for i in range(num):
        name = input("What is the persons name?: ")
        age = input("What is the persons age?: ")
        computing = input("What is the persons Computing score?: ")
        maths = input("What is the persons Maths score?: ")
        english = input("What is the persons English score?: ")
        newPerson = Person(name, age, computing, maths, english)

        people.append(newPerson)

    return people

您的意思是先收集参数,然后将它们提供给新实例吗

def getPeople(num):
人=[]
对于范围内的i(num):
姓名=输入(“姓名是什么?:”)
年龄=输入(“人员年龄:”)
计算=输入(“人员计算分数是多少?”)
数学=输入(“人的数学分数是多少?”)
英语=输入(“个人英语分数是多少?”)
people.append(Person(姓名、年龄、计算机、数学、英语))
返回人

请使用完整的错误回溯更新您的问题。您创建的对象没有其类参数,您必须使用
person(姓名、年龄等)调用person对象。
您调用person()时没有参数,但有五个必需参数。您应该发布错误消息,但我怀疑它表明缺少5个必需的位置参数?