mee*_*mee 3 python class typeerror
我知道这个问题已被问过几次,但没有一个能够为我提供解决方案.我看了这些:
我所要做的就是为"生存游戏"创建两个类,就像一个非常糟糕的我的世界版本.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
Run Code Online (Sandbox Code Playgroud)
现在为了完整的错误:
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)
Run Code Online (Sandbox Code Playgroud)
我只是想让课程有效.
您使用的代码如下:
player = Player()
Run Code Online (Sandbox Code Playgroud)
这是一个问题,因为__init__必须由name根据您的代码调用的一个参数提供.因此,要解决您的问题,只需为Player构造函数提供一个名称,您就可以了:
player = Player('sdfasf')
Run Code Online (Sandbox Code Playgroud)