代码给出 TypeError
The code is giving TypeError
class GameCharacter(object):
__name = ""
__health = 0
__experience = 0
def __init__(self, name, health, experience):
self.__name = name
self.__health = health
self.__experience = experience
def getName(self):
return self.__name
def adventure( e ):
self.__experience += e
def __str__(self):
return self.__name+ " is a " + str(type(self)) + " with: \n HEALTH: " \
+ str(self.__health) + " and EXPERIENCE: " + str(self.__experience)
gc = GameCharacter("gc", 10, 20)
print(gc)
class Wizard(GameCharacter):
__spells = {}
__knowledge = 0
def __init__(self, name, health, experience, spells, knowledge):
super().__init__(name, health, experience)
self.__spells = spells
self.__knowledge = knowledge
def __str__(self):
return (super().__str__() + "\n SPELLS: " + str(self.__spells) \
+ " and KNOWLEDGE: " + str(self.__knowledge))
def learn( k ):
self.__knowledge += k
# This method returns damage amount which is calculated as follows:
# select a random spell from the dictionary of spells using
# the knowledge value as the range, damage = potency of spell
def castSpell():
#?????
pass
class Warrior(GameCharacter):
__weapons = {}
__skill = 0
def __init__(self, name, health, experience, spells, skill):
pass
def __str__(self):
return "This needs to be implemented..."
#this method updates the value of __skill by s
def train( s ):
pass
# This method returns damage amount which is calculated as follows:
# select a random weapon from the dictionary of weapons using
# the skill value as the range, damage = strength of weapon
def useWeapon():
pass
wiz1 = Wizard("Wizzy", 100, 50, {}, 5 )
print(wiz1)
warr1 = Warrior("Warry", 100, 75, {}, 10)
print (warr1)
class AdventureGame():
#initializes the game characters
def __init__(self, wizard, warrior):
pass
#returns a string representing information about the game characters
def __str__(self):
pass
#generates a random number in range 0-5 for wizard: wizard gains knowledge by this much
#generates a random number in range 0-5 for warrior: warrior gains strength by this much
def adventure(self):
pass
#generates a random number in range 0-5 for wizard: wizard loses knowledge by this much
#generates a random number in range 0-5 for warrior: warrior loses strength by this much
def peril(self):
pass
#wizard casts a spell
#warrior draws a weapon
#return the winner - who has more health, or tie --> wizard|warrior|tie
def battle(self):
pass
我试图将此代码编译为 py 2.7.11,但它给出了此输出
gc is a <class '__main__.GameCharacter'> with:
HEALTH: 10 and EXPERIENCE: 20
Traceback (most recent call last):
File "C:/Users/Muhammad Danial/Desktop/sample.py", line 76, in <module>
wiz1 = Wizard("Wizzy", 100, 50, {}, 5 )
File "C:/Users/Muhammad Danial/Desktop/sample.py", line 34, in __init__
super().__init__(name, health, experience)
TypeError: super() takes at least 1 argument (0 given)
谁来指出我的错误。我认为代码中缺少一部分。可能是用最新版本编写的导致此错误。我试图在 super 中传递参数,但每次都遇到一种新的错误。
在 Python2 super()
中引用 cls
(class) 和 self
(class 实例) 作为参数。
看这里:
super() should not be considered the standard way of calling a method of the base class. This did not change with Python 3.x. The only thing that changed is that you don't need to pass the arguments self, cls in the standard case that self is the first parameter of the current function and cls is the class currently being defined.
现在,当我 运行 您在 Python3 中的代码时,我没有收到任何错误。
此处调用 super
的正确方法是使用以下参数:
super(Wizard, self)
因为 python2 中的 super 需要参数(在 python3 中可以只做 super()
)
查看 docs for super 了解更多详情
编辑:看来这个错误在您的代码中重复了多次,请记住,由于同样的问题,有些事情将无法正常工作。确保只在需要时使用 super
并正确使用
此代码适用于 Python 3.x。
尝试Python3.X,或修改代码
super().__init__(name, health, experience)
至
super(Wizard, self).__init__(name, health, experience)
在2.x中,你应该为super()的参数写'own class name'和'self'。
class GameCharacter(object):
__name = ""
__health = 0
__experience = 0
def __init__(self, name, health, experience):
self.__name = name
self.__health = health
self.__experience = experience
def getName(self):
return self.__name
def adventure( e ):
self.__experience += e
def __str__(self):
return self.__name+ " is a " + str(type(self)) + " with: \n HEALTH: " \
+ str(self.__health) + " and EXPERIENCE: " + str(self.__experience)
gc = GameCharacter("gc", 10, 20)
print(gc)
class Wizard(GameCharacter):
__spells = {}
__knowledge = 0
def __init__(self, name, health, experience, spells, knowledge):
super().__init__(name, health, experience)
self.__spells = spells
self.__knowledge = knowledge
def __str__(self):
return (super().__str__() + "\n SPELLS: " + str(self.__spells) \
+ " and KNOWLEDGE: " + str(self.__knowledge))
def learn( k ):
self.__knowledge += k
# This method returns damage amount which is calculated as follows:
# select a random spell from the dictionary of spells using
# the knowledge value as the range, damage = potency of spell
def castSpell():
#?????
pass
class Warrior(GameCharacter):
__weapons = {}
__skill = 0
def __init__(self, name, health, experience, spells, skill):
pass
def __str__(self):
return "This needs to be implemented..."
#this method updates the value of __skill by s
def train( s ):
pass
# This method returns damage amount which is calculated as follows:
# select a random weapon from the dictionary of weapons using
# the skill value as the range, damage = strength of weapon
def useWeapon():
pass
wiz1 = Wizard("Wizzy", 100, 50, {}, 5 )
print(wiz1)
warr1 = Warrior("Warry", 100, 75, {}, 10)
print (warr1)
class AdventureGame():
#initializes the game characters
def __init__(self, wizard, warrior):
pass
#returns a string representing information about the game characters
def __str__(self):
pass
#generates a random number in range 0-5 for wizard: wizard gains knowledge by this much
#generates a random number in range 0-5 for warrior: warrior gains strength by this much
def adventure(self):
pass
#generates a random number in range 0-5 for wizard: wizard loses knowledge by this much
#generates a random number in range 0-5 for warrior: warrior loses strength by this much
def peril(self):
pass
#wizard casts a spell
#warrior draws a weapon
#return the winner - who has more health, or tie --> wizard|warrior|tie
def battle(self):
pass
我试图将此代码编译为 py 2.7.11,但它给出了此输出
gc is a <class '__main__.GameCharacter'> with:
HEALTH: 10 and EXPERIENCE: 20
Traceback (most recent call last):
File "C:/Users/Muhammad Danial/Desktop/sample.py", line 76, in <module>
wiz1 = Wizard("Wizzy", 100, 50, {}, 5 )
File "C:/Users/Muhammad Danial/Desktop/sample.py", line 34, in __init__
super().__init__(name, health, experience)
TypeError: super() takes at least 1 argument (0 given)
谁来指出我的错误。我认为代码中缺少一部分。可能是用最新版本编写的导致此错误。我试图在 super 中传递参数,但每次都遇到一种新的错误。
在 Python2 super()
中引用 cls
(class) 和 self
(class 实例) 作为参数。
看这里:
super() should not be considered the standard way of calling a method of the base class. This did not change with Python 3.x. The only thing that changed is that you don't need to pass the arguments self, cls in the standard case that self is the first parameter of the current function and cls is the class currently being defined.
现在,当我 运行 您在 Python3 中的代码时,我没有收到任何错误。
此处调用 super
的正确方法是使用以下参数:
super(Wizard, self)
因为 python2 中的 super 需要参数(在 python3 中可以只做 super()
)
查看 docs for super 了解更多详情
编辑:看来这个错误在您的代码中重复了多次,请记住,由于同样的问题,有些事情将无法正常工作。确保只在需要时使用 super
并正确使用
此代码适用于 Python 3.x。
尝试Python3.X,或修改代码
super().__init__(name, health, experience)
至
super(Wizard, self).__init__(name, health, experience)
在2.x中,你应该为super()的参数写'own class name'和'self'。