【发布时间】:2015-07-17 03:49:26
【问题描述】:
我知道这个问题已经被问过好几次了,但没有人能够为我的问题提供解决方案。我读了这些:
__init__() takes exactly 2 arguments (1 given)?
class __init__() takes exactly 2 arguments (1 given)
我要做的就是为“生存游戏”创建两个类,就像非常糟糕的 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)
我只想让课程正常工作。
【问题讨论】:
-
Player接受name参数,但您什么都不传递。 -
没错,您传递了隐含的
self参数,但没有传递name参数。 -
稍后在文本文件中我实际上做了 x = Player("string") 然后从那里继续。我只是忘了在这里包含那一点
-
你链接的两个问题完美地解释了这个问题。不知道你是怎么从那里弄出来的......