【问题标题】:Python using a class within a function and appending to a listPython 在函数中使用类并附加到列表
【发布时间】:2021-01-28 04:03:03
【问题描述】:

我对 Python 和类比较陌生。

我想创建一个具有nameage 等特征的 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


【解决方案1】:

您需要先收集参数,然后将它们传递给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?: ")

        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)]

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      __init__() 方法的参数指定您在调用Person() 时必须提供的参数(除了自动传递self)。所以你需要在那里传递所有的属性值,而不是在创建人之后分配它们。

      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
      

      【讨论】:

        【解决方案4】:

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

        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
        

        【讨论】:

          猜你喜欢
          • 2020-10-25
          • 2022-01-13
          • 2019-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-30
          • 2020-07-21
          相关资源
          最近更新 更多