Python: TypeError: __init__() takes exactly 2 argu

2019-02-27 12:58发布

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

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

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:

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条回答
萌系小妹纸
2楼-- · 2019-02-27 13:06

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

player = Player()

to

player = Player('somename')

to get the program up and running.

查看更多
叼着烟拽天下
3楼-- · 2019-02-27 13:15

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:

player = Player('sdfasf')
查看更多
Rolldiameter
4楼-- · 2019-02-27 13:23

__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

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.

查看更多
5楼-- · 2019-02-27 13:24

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__.

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:

self.exampleOne()
self.exampleTwo('input1', 'input2')
查看更多
登录 后发表回答