Python:TypeError:__ init __()只需2个参数(给定1个)

时间:2021-12-24 04:45:20

I know this question has been asked several times, but none have managed to provide me with a solution to my issue. I read these:

我知道这个问题曾被问过几次,但没有一个能够为我提供解决方案。我看了这些:

__init__() takes exactly 2 arguments (1 given)?

__init __()只需要2个参数(给定1个)?

class __init__() takes exactly 2 arguments (1 given)

class __init __()只需2个参数(给定1个)

All I am trying to do is create two classes for a "survival game" much like a very crappy version of minecraft. Bellow is the full code for the two classes:

我所要做的就是为“生存游戏”创建两个类,就像一个非常糟糕的我的世界版本。 Bellow是这两个类的完整代码:

class Player:
    '''
    Actions directly relating to the player/character.
    '''
    def __init__(self, name):
        self.name = name
        self.health = 10
        self.shelter = False

    def eat(self, food):
        self.food = Food
        if (food == 'apple'):
            Food().apple()

        elif (food == 'pork'):
            Food().pork()

        elif (food == 'beef'):
            Food().beef()

        elif (food == 'stew'):
            Food().stew()

class Food:
    '''
    Available foods and their properties.
    '''
    player = Player()

    def __init__(self):
        useless = 1
        Amount.apple = 0
        Amount.pork = 0
        Amount.beef = 0
        Amount.stew = 0

    class Amount:   
        def apple(self):
            player.health += 10

        def pork(self):
            player.health += 20

        def beef(self):
            player.health += 30

        def stew(self):
            player.health += 25      

And now for the full error:

现在为了完整的错误:

Traceback (most recent call last):
  File    "/home/promitheas/Desktop/programming/python/pygame/Survive/survive_classe  s.py", line 26, in <module>
    class Food:
  File     "/home/promitheas/Desktop/programming/python/pygame/Survive/survive_classe    s.py", line 30, in Food
    player = Player()
TypeError: __init__() takes exactly 2 arguments (1 given)

I just want to make the classes work.

我只是想让课程有效。

4 个解决方案

#1


The code you used is as follows:

您使用的代码如下:

player = Player()

This is an issue since the __init__ must be supplied by one parameter called name according to your code. Therefore, to solve your issue, just supply a name to the Player constructor and you are all set:

这是一个问题,因为__init__必须由一个名为name的参数根据您的代码提供。因此,要解决您的问题,只需为Player构造函数提供一个名称,您就可以了:

player = Player('sdfasf')

#2


The problem is that the Class Player's __init__ function accepts a name argument while you are initializing the Class instance. The first argument, self is automatically handled when you create a class instance. So you have to change

问题是Class Player的__init__函数在初始化Class实例时接受name参数。创建类实例时会自动处理第一个参数self。所以你必须改变

player = Player()

to

player = Player('somename')

to get the program up and running.

让程序运行起来。

#3


__init__() is the function called when the class is instantiated. So, any arguments required by __init__ need to be passed when creating an instance. So, instead of

__init __()是实例化类时调用的函数。因此,在创建实例时需要传递__init__所需的任何参数。所以,而不是

player = Player()

use

player = Player("George")

The first argument is the implicit self, which doesn't need to be included when instantiating. name, however, is required. You were getting the error because you weren't including it.

第一个参数是隐式self,在实例化时不需要包含它。但是,名称是必需的。您收到错误是因为您没有包含它。

#4


Your code know expects that you input something in __init__ which you don't.
I have made a simple example below that does give you an idea where the error __init__() takes exactly 2 arguments (1 given) is coming from.
What I did is I made a definition where I give input to useless.
And I am calling that definition from __init__.

您的代码知道您希望在__init__中输入您不需要的内容。我在下面做了一个简单的例子,它确实可以让你知道错误__init __()需要两个参数(给定的1个)来自哪里。我做的是我做了一个定义,我给无用的输入。我从__init__中调用了这个定义。

Example code:

class HelloWorld():
    def __init__(self):
        self.useThis(1)
    def useThis(self, useless):
        self.useless = useless
        print(useless)

# Run class 
HelloWorld()

If you have a definition like def exampleOne(self) it doesn't expect any input. It just looks a t itself.
But def exampleTwo(self, hello, world) expects two inputs.
So to call these two you need:

如果你有一个像def exampleOne(self)这样的定义,它不会期望任何输入。它只是看起来本身。但def exampleTwo(self,hello,world)需要两个输入。所以要称这两个你需要:

self.exampleOne()
self.exampleTwo('input1', 'input2')

#1


The code you used is as follows:

您使用的代码如下:

player = Player()

This is an issue since the __init__ must be supplied by one parameter called name according to your code. Therefore, to solve your issue, just supply a name to the Player constructor and you are all set:

这是一个问题,因为__init__必须由一个名为name的参数根据您的代码提供。因此,要解决您的问题,只需为Player构造函数提供一个名称,您就可以了:

player = Player('sdfasf')

#2


The problem is that the Class Player's __init__ function accepts a name argument while you are initializing the Class instance. The first argument, self is automatically handled when you create a class instance. So you have to change

问题是Class Player的__init__函数在初始化Class实例时接受name参数。创建类实例时会自动处理第一个参数self。所以你必须改变

player = Player()

to

player = Player('somename')

to get the program up and running.

让程序运行起来。

#3


__init__() is the function called when the class is instantiated. So, any arguments required by __init__ need to be passed when creating an instance. So, instead of

__init __()是实例化类时调用的函数。因此,在创建实例时需要传递__init__所需的任何参数。所以,而不是

player = Player()

use

player = Player("George")

The first argument is the implicit self, which doesn't need to be included when instantiating. name, however, is required. You were getting the error because you weren't including it.

第一个参数是隐式self,在实例化时不需要包含它。但是,名称是必需的。您收到错误是因为您没有包含它。

#4


Your code know expects that you input something in __init__ which you don't.
I have made a simple example below that does give you an idea where the error __init__() takes exactly 2 arguments (1 given) is coming from.
What I did is I made a definition where I give input to useless.
And I am calling that definition from __init__.

您的代码知道您希望在__init__中输入您不需要的内容。我在下面做了一个简单的例子,它确实可以让你知道错误__init __()需要两个参数(给定的1个)来自哪里。我做的是我做了一个定义,我给无用的输入。我从__init__中调用了这个定义。

Example code:

class HelloWorld():
    def __init__(self):
        self.useThis(1)
    def useThis(self, useless):
        self.useless = useless
        print(useless)

# Run class 
HelloWorld()

If you have a definition like def exampleOne(self) it doesn't expect any input. It just looks a t itself.
But def exampleTwo(self, hello, world) expects two inputs.
So to call these two you need:

如果你有一个像def exampleOne(self)这样的定义,它不会期望任何输入。它只是看起来本身。但def exampleTwo(self,hello,world)需要两个输入。所以要称这两个你需要:

self.exampleOne()
self.exampleTwo('input1', 'input2')