Python:TypeError:__init __()恰好接受2个参数(给定1个) [英] Python: TypeError: __init__() takes exactly 2 arguments (1 given)

查看:88
本文介绍了Python:TypeError:__init __()恰好接受2个参数(给定1个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经问过几次了,但是没有一个人能为我提供解决问题的方法。我读了这些:

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 __()正好接受2个参数(给定1个)?

类__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:

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)

我只想使上课。

推荐答案

您使用的代码如下:

player = Player()

__ init __ 必须根据您的代码由一个名为 name 的参数提供。因此,要解决您的问题,只需为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')

这篇关于Python:TypeError:__init __()恰好接受2个参数(给定1个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆