0

Hi below is the relevant code for my problem:

    class Player:
        def __init__(self, name, x, y, isEvil):
            self.health = 50
            self.attack = randint(1, 5)
            self.name = name
            self.x = x
            self.y = y
            self.isEvil = isEvil

        def checkIsAlive(self):
            if self.health <= 0:
            return False
        else:
            return True

    # implicit in heritance. if the child doesn't have a function that the parent has
    # the functions of the parent can be called as if it were being called on the
    # parent itself

    class Enemy(Player):


        #def __init__(self, name, x, y, isEvil):
        # self.health = 50
        # self.name = name
        # self.x = x
        # self.y = y
        pass

and a little more code:

    e = Enemy('Goblin', 10, 11, True)

    p = Player('Timmeh', 0, 1, False)
    isLight()

    while True:
        if p.checkIsAlive() == True and e.checkIsALive() == True:
            fight()

        else:
            if p.checkIsAlive() == True:
                print('%s is victorious!!! %s survived with %s health points.' % (p.name, p.name, p.health))
            else:
                print('%s shrieks in its bloodlust!!! %s has %s health points' % (e.name, e.name, e.health))

however when i try and run this I get the following error:

    Traceback (most recent call last):
    File "<string>", line 420, in run_nodebug
    File "C:\Python33\practice programs\textstrat\classees.py", line 94, in <module>
    if p.checkIsAlive() == True and e.checkIsALive() == True:
    AttributeError: 'Player' object has no attribute 'checkIsAlive'

However when using the interactive console I can do this:

    if p.checkIsAlive() == True and e.checkIsAlive() == True:
    ...     print('they are')
    ...     
    they are

all I want to do is call the boolean values for checkIsAlive to determine whether the two objects fight. It works in every other respect and I could just use: if p.health <= 0 or e.health <= 0: however that would make my checkIsAlive() method pretty useless when i would also want to be able to recycle as much code ass possible. I really can't figure out why it is behaving this way and would sure love to understand it. Thanks in advance for your input.

Coned_Stoding
  • 135
  • 1
  • 3
  • 10
  • 1
    It's a typo. You have `checkIsALive(e)`. Note the capital 'L'. – dano Sep 05 '14 at 03:59
  • @dano it's not even getting there, as the error message has the correct capitalization. Note: `checkAlive(p) != p.checkAlive()` – aruisdante Sep 05 '14 at 04:00
  • It also looks like your example code and your *actual* code are not the same. Your example uses: `if p.checkIsAlive() == True and e.checkIsALive() == True`. But your traceback shows `if checkIsAlive(p) == True and checkIsALive(e) == True:`, which definitely isn't what you want. – dano Sep 05 '14 at 04:00
  • @aruisdante The error message actually has the wrong capitalization on one of the calls, too. (`checkIsALive(e)`) – dano Sep 05 '14 at 04:01
  • Hi guys sorry yes, I got rid of the p and e in the brackest i copied an old traceback by mistake. The typo was apparently causing the error tyvm, annoyed with myself for missing that one. – Coned_Stoding Sep 05 '14 at 04:01

1 Answers1

0

As was pointed out swiftly in the comments above. I had missed a typo in the attribute name checkIsAlive.

Coned_Stoding
  • 135
  • 1
  • 3
  • 10