4

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.

Community
  • 1
  • 1
mee
  • 489
  • 5
  • 11
  • 19
  • 1
    `Player` takes a `name` parameter, but you don't pass it anything. – Peter Wood May 06 '15 at 20:02
  • Exactly, you pass the implied `self` argument but not the `name` argument. – Malik Brahimi May 06 '15 at 20:04
  • Later on in the text file I actually do x = Player("string") and then proceed from there. I just forgot to include that bit here – mee May 06 '15 at 20:06
  • 2
    The two questions you linked explain the problem perfectly. Not sure how you didn't figure it out from there... – MattDMo May 06 '15 at 20:06
  • possible duplicate of [\_\_init\_\_() takes exactly 2 arguments (1 given)?](http://stackoverflow.com/questions/25805194/init-takes-exactly-2-arguments-1-given) – abcd May 06 '15 at 21:22

4 Answers4

6

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')
Jerry Ajay
  • 1,084
  • 11
  • 26
  • 3
    Why the down vote? The question was to get the two classes working and this is the most simplest answer possible. – Jerry Ajay May 06 '15 at 20:11
  • I too disagree with the downvote but ... Henceforth 1. Explain why he should do so 2. DONT use sms language while answering 3. Have some meaningful names. Correct these 3, the downvoter will surely remove his downvote – Bhargav Rao May 06 '15 at 20:13
  • 1
    When you hover over the downvote button, the popup text says "*This answer is not useful.*" Some possible reasons: 1) you're using txt-speak, which some people find very annoying and ignorant, 2) you don't explain anything, just say "*do this and it will work*", and 3) it was posted well after two other answers were, both of which explained the reason for the error and how to fix it. – MattDMo May 06 '15 at 20:21
  • Ok. I edited my answer to be slightly more descriptive. Thanks for both your feedbacks. – Jerry Ajay May 06 '15 at 20:25
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

player = Player()

to

player = Player('somename')

to get the program up and running.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
2

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

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Instead of typing Player("George") in the Food class, how can I make it so that Player(name) works and gets the value for name which I enter when I run the actual Player class with an argument? – mee May 06 '15 at 20:20
  • @mee I also don't fully understand what you are trying to do. Do you want to make a variable called "name" that can be used trough all classes? If yes, you make a variable a global variable. Then every class/definition can use it. Or you can pass th variable on to another class by using inheritence. – Tenzin May 06 '15 at 20:30
  • In the complete file, at the end I have these lines: x = Player("string") print x.name print x.health x.eat('apple') print x.health Is there a way for me to initialize Player in the Food class without having to write "string" a second time, but to use the value of x = Player("string")? I hope its clearer now – mee May 06 '15 at 20:31
0

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')
Tenzin
  • 2,415
  • 2
  • 23
  • 36
  • How does this answer the question? – MattDMo May 06 '15 at 20:14
  • 1
    It exmplains where the error "__init__() takes exactly 2 arguments (1 given)?" is comming from. And why it is giving that error. That was the initial question before the topic starter post got edited. – Tenzin May 06 '15 at 20:16