0
#Here is the code I currently have:
class Character(object):
    def __init__(self, currentHealth, maxHealth, damage, defence, agility):
        self.currentHealth = currentHealth
        self.maxHealth = maxHealth
        self.damage = damage
        self.defence = defence
        self.agility = agility

class Enemy(Character):
    def __init__(self, drop):
        Character.__init__(self, currentHealth, maxHealth, damage, defence, agility)
        self.drop = drop

    def checkHealth(self):
        print("The enemies health is: " + str(self.currentHealth))

    def inspectStats(self):
        print("The enemies health is: " + str(self.currentHealth))
        print("The enemies attack is: " + str(self.damage))
        print("The enemies defence is: " + str(self.defence))
        print("The enemies evasiveness is: " + str(self.agility))

class Player(Character):
    def __init__(self, currentHealth, maxHealth, damage, defence, agility, gold):
        self.currentHealth = currentHealth
        self.maxHealth = maxHealth
        self.damage = damage
        self.defence = defence
        self.agility = agility
        self.gold = gold

    def checkHealth(self):
        print("Your current health is: " + str(self.currentHealth))

    def inspectStats(self):
        print("Your current health is: " + str(self.currentHealth))
        print("Your max health is: " + str(self.maxHealth))
        print("Your attack is: " + str(self.damage))
        print("Your defence is: " + str(self.defence))
        print("Your agility is: " + str(self.agility))
        print("Your gold is: " + str(self.gold))

bob = Player(15, 15, 5, 6, 7, 70)
spider = Enemy(10, 10, 2, 1, 5, 'silk')
spider.inspectStats()

I haven't been able to get this code working, I have searched online and have found mild success with this website: http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/ It appears, however, that the code still isn't working. It would be greatly appreciated if someone could tell me how I am supposed to do inheritance. (I know that there is going to be a subclass of enemy so it would be helpful if that was taken into consideration).

Micah Lehman
  • 81
  • 1
  • 1
  • 8
  • 5
    Could you be more specific? What is not working? What errors are you getting? What is your expected output? – berkelem Nov 29 '18 at 14:45
  • Sorry, I am new to this. I expect to be able to print out the variables I am putting into the Enemy class, however it keep's telling me that I supplied 7 arguments and it could only take 2. – Micah Lehman Nov 29 '18 at 14:57
  • but the `Enemy` class takes only 2 arguments – Take_Care_ Nov 29 '18 at 15:03

2 Answers2

1

Ok , so , your child class which is Enemy got in method definition that it only takes 2 arguments self and drop. So if You want to make it able to recieve more params you must provide some placeholder for it. The naive way is just write all the needed params in place. But lats say in future you will extend the number of the params the Character class is taking. To make the solution more freindly we can add the *args varibale as param. This vairable (args isnt obligatory name the crucial here is the * operator standing before it) will "catch" all the params which you provide after the drop param. The star operator doesn't care how many params You pass. It will try to take as many as possible. These stored params can now be unpacked to the parent __init__ method like this :

class Enemy(Character):
    # the *args will "catch" all the positional 
    # parameters passed to this class 
    def __init__(self, drop, *args):

        # now we are using unpacking to 
        # apply all positional params holded in args to parent init method
        Character.__init__(self, *args)
        self.drop = drop

# Now You must remeber to pass the the init the  
# child argument which is drop 
# + all the parent class positional args (currentHealth, ...)

enemy = Enemy(True,...)
Take_Care_
  • 1,957
  • 1
  • 22
  • 24
  • Please check one more time, the first version was with `kwargs` coz I didnt notice you don't use keyword params but positional args – Take_Care_ Nov 29 '18 at 15:42
1

Your Enemy init function doesn't have the right number of parameters. Try this:

class Enemy(Character):
    def __init__(self, currentHealth, maxHealth, damage, defence, agility, drop):
        super(Enemy, self).__init__(currentHealth, maxHealth, damage, defence, agility)
        self.drop = drop

For more details about the "super" keyword, you can go there

Akarius
  • 388
  • 1
  • 7