Python:TypeError:\uuuu init\uuuuu()正好接受2个参数(给定1个)

Python:TypeError:\uuuu init\uuuuu()正好接受2个参数(给定1个),python,class,typeerror,Python,Class,Typeerror,我知道这个问题已经被问过好几次了,但没有一次能为我的问题提供解决方案。我读到: 我所要做的就是为一个“生存游戏”创建两个类,就像一个非常糟糕的minecraft版本。下面是两个类的完整代码: class Player: ''' Actions directly relating to the player/character. ''' def __init__(self, name): self.name = name self

我知道这个问题已经被问过好几次了,但没有一次能为我的问题提供解决方案。我读到:

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

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      
现在来看完整的错误:

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)
回溯(最近一次呼叫最后一次):
文件“/home/promitheas/Desktop/programming/python/pygame/Survive/Survive_classe s.py”,第26行,在
类别食物:
文件“/home/promitheas/Desktop/programming/python/pygame/Survive/Survive_classe s.py”,第30行,食品中
player=player()
TypeError:\uuuu init\uuuuuu()正好接受2个参数(给定1个)

我只是想让这些课程发挥作用

问题在于,在初始化类实例时,类
Player
\uuuu init\uuu
函数接受
名称
参数。创建类实例时,会自动处理第一个参数
self
。所以你必须改变

player = Player()

使程序启动并运行。

\uuuu init\uuu()
是实例化类时调用的函数。因此,在创建实例时需要传递
\uuuu init\uuuu
所需的任何参数。因此,与其

player = Player()
使用


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

您使用的代码如下所示:

player = Player()
这是一个问题,因为根据您的代码,
\uuu init\uu
必须由一个名为
name
的参数提供。因此,要解决您的问题,只需向播放器构造函数提供一个名称,您就可以:

player = Player('sdfasf')

您的code know希望您在
\uuuu init\uuuu
中输入您不需要的内容
我在下面做了一个简单的例子,让您知道错误
\uuuu init\uuuuuu()正好取了2个参数(给定1个)
的来源
我所做的是我做了一个定义,在这里我给
无用的
提供输入
我从
\uuuu init\uuuu
中调用这个定义

示例代码:

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

# Run class 
HelloWorld()
如果您有一个类似于
def exampleOne(self)
的定义,它不需要任何输入。它看起来就像一个t本身
但是
def exampleTwo(self,hello,world)
需要两个输入
因此,要称呼这两个,您需要:

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

Player
接受一个
name
参数,但您不传递任何东西。确切地说,您传递的是隐含的
self
参数,而不是
name
参数。稍后在文本文件中,我实际执行x=Player(“字符串”),然后从那里开始。我只是忘了在这里加上这一点。你把两个问题联系起来,完美地解释了这个问题。不知道你怎么没从那里弄明白…可能重复为什么投票失败?问题是让两个班级都能正常工作,这是最简单的答案。我也不同意否决票,但。。。从今以后1。解释他为什么要这样做。回答3时不要使用短信语言。有一些有意义的名字。纠正这3个错误,downvoter肯定会删除他的downvote当你将鼠标悬停在downvote按钮上时,弹出的文本显示“这个答案没有用”。可能的原因有:1)你使用的是txt speak,有些人觉得这很烦人和无知,2)你不解释任何事情,只需说“这样做,它就会起作用”,3)它是在其他两个答案发布之后发布的,这两个答案都解释了错误的原因以及如何修复错误。我编辑了我的答案,使之更具描述性。谢谢你们的反馈。这是如何回答这个问题的?它表示错误“\uuuu init\uuuuuuu()正好取了2个参数(1个给定)?”是从哪里产生的。以及为什么会出现这种错误。这是编辑主题初学者帖子之前的第一个问题。我不在Food类中键入Player(“George”),如何让Player(name)工作并获取我在运行实际的Player类时输入的name值?我也不完全理解你在尝试做什么。是否要生成一个名为“name”的变量,该变量可用于所有类?如果是,则将变量设置为全局变量。然后每个类/定义都可以使用它。或者您可以使用继承将th变量传递给另一个类。在完整的文件中,最后我有以下行:x=Player(“string”)print x.name print x.health x.eat(“apple”)print x.health是否有一种方法可以让我在食品类中初始化Player,而不必再次写入“string”,而是使用x=Player的值(“字符串”)?我希望它现在更清晰
class HelloWorld():
    def __init__(self):
        self.useThis(1)
    def useThis(self, useless):
        self.useless = useless
        print(useless)

# Run class 
HelloWorld()
self.exampleOne()
self.exampleTwo('input1', 'input2')