【发布时间】:2021-01-28 04:03:03
【问题描述】:
我对 Python 和类比较陌生。
我想创建一个具有name、age 等特征的 Person 对象。然后我想将许多不同的 people 对象附加到一个列表中。我在函数中引用类时遇到问题,因为它正在寻找参数,但当我输入参数时似乎不起作用。 Line 13.
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
def getPeople(num):
people = []
for i in range(num):
newPerson = Person()
newPerson.name = input("What is the persons name?: ")
newPerson.age = input("What is the persons age?: ")
newPerson.computing = input("What is the persons Computing score?: ")
newPerson.maths = input("What is the persons Maths score?: ")
newPerson.english = input("What is the persons English score?: ")
people.append(newPerson)
return people
getPeople(5)
Traceback (most recent call last):
File "C:\Users\records.py", line 24, in <module>
getPeople(5)
File "C:\Users\srecords.py", line 13, in getPeople
newPerson = Person()
TypeError: __init__() missing 5 required positional arguments: 'name', 'age', 'computing', 'maths', and 'english'
[Finished in 0.2s with exit code 1]
【问题讨论】:
-
请使用完整的错误回溯更新您的问题。
-
你创建了没有类参数的对象,你必须用
Person(name, age,..)调用person对象 -
你调用 Person() 时没有参数,但它有五个必需参数。您应该发布错误消息,但我怀疑它说明缺少 5 个必需的位置参数?
标签: python list function class