2
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

I tried to compile this code to the py 2.7.11 but it is giving this output

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)

Anybody here to point out my mistake. I think there is a missing part in the code. May be it is written in latest version which caused this error. I tried to pass the arguments in the super but every time faced a new kind of error.

  • What kind of arguments did you try to pass in `super` – Ofer Sadan Aug 08 '17 at 05:07
  • Tried to pass local –  Aug 08 '17 at 05:11
  • What kind of arguments should I pass and how many to get required output? –  Aug 08 '17 at 05:12
  • 5
    Why would you use Python 2 for new developments instead of Python 3 anyway? – Fabien Aug 08 '17 at 05:13
  • 1
    What's the point of all those class attributes like `__name`, `__health`, etc, that get shadowed by instance attributes in the `__init_` methods? And why are you using [name mangling](https://docs.python.org/3/tutorial/classes.html#private-variables)? – PM 2Ring Aug 08 '17 at 05:20
  • You shouldn't be using name-mangling here. It is almost certainly the *opposite* of what you want. Also, stop writing getters and setters, e.g. `getName` – juanpa.arrivillaga Aug 08 '17 at 05:54

3 Answers3

1

In Python2 super() takes reference of cls (class) and self (class instance) as arguments.

Take a look here:

https://stackoverflow.com/a/5066411/6654077

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.

Now, when I run your code in Python3, I got no error.

Fabien
  • 4,862
  • 2
  • 19
  • 33
1

The proper way to call super here is with the following arguments:

super(Wizard, self)

Because super in python2 needs the arguments (in python3 it's possible to do just super())

Take a look at the docs for super for more details

Edit: It appears that this mistake is repeated more times in your code, keep that in mind that some things will not work because of the same problem. Make sure you use super only when needed and use it properly

Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62
0

this code works well on Python 3.x.

Try Python 3.X, or fix the code

super().__init__(name, health, experience)

to

super(Wizard, self).__init__(name, health, experience)

in 2.x, you should write 'own class name' and 'self' for argument of super().