0

I have this code that gives me weird errors. Below you should see that code. First of i have my CombatEngine that basically just simulates a shoot out between me the player and the list of enemies. In the class TheBridge i make a instance of the combatengine and give it a list of enemies in the constructor(i code mostly in c#) but when i run the the combat method on the engine an error will be returned

AttributeError: type object 'GothonTrooper' has no attribute 'health'

I really dont understand how that is possible when its clear that health is defined for the GothonTrooper class. I suppose the error occurs in the combat method itself some point around when the individual enemy is found from the randint function.

class TheBridge(Scene):
    def __init__(self):
        enemies = [GothonTrooper(), GothonTrooper(), GothonTrooper(), GothonTrooper, GothonTrooper()]
        self.the_bridge_combat = CombatEngine(enemies)

        ...


class CombatEngine(object):
    def __init__(self, enemies):
        self.enemies = enemies

    while len(self.enemies) != 0:       
        enemy = self.enemies[randint(0, len(self.enemies)-1)] 
        print "You shoot at the Gothon." 
        hit_or_miss = PLAYER.attack() 
        if hit_or_miss >= 5:
            print "The shot hits the Gothon!"
            enemy.health -= 10
            print "The Gothon is down to %s health" % enemy.health
        ...


class GothonTrooper(Humanoid):
    def __init__(self):
        self.health = 100

    def attack(self):
        return randint(1,10)
2
  • Can you please show the full traceback? Commented Dec 15, 2014 at 3:01
  • 5
    Is it a typo that you did not include the parenthesis for one of your GothonTrooper in your enemies list? Commented Dec 15, 2014 at 3:01

2 Answers 2

1

Assuming what you have provided in your code is not a typo, the missing () in one of your GothonTrooper objects in your enemies list is the culprit. Without it, that object is never instatiated. Therefore that item will not yet have the health attribute.

To better illustrate where the problem is stemming from, the below example uses the dir method to return properties available on that object, (notice that health is missing on the second print line)

>>> class Trooper():
    def __init__(self):
        self.health = "90%"


>>> enemies = [Trooper(), Trooper]
>>> for enemy in enemies:
       print(dir(enemy))


[..., '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'health']
[..., '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
Sign up to request clarification or add additional context in comments.

1 Comment

What a blunder Im not used to be able to do that in C#, thanks for the help !
1

Sorry,my English is poor. Look at your define,the fourth GothonTrooper don't have () ,so as to AttributeError: type object GothonTrooper has no attribute health

enemies = [GothonTrooper(), GothonTrooper(), GothonTrooper(), GothonTrooper, GothonTrooper()]

1 Comment

thanks for the help it did indeed fix my issue but i gave the answer to Anthony as he was the first with the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.