I need help with this code please.It involves cavern survival

loop:
enemy = self.findNearestEnemy()
flag = self.findFlag()
if flag:
self.pickUpFlag(flag)
if enemy:

        if hero.health < 200:
            hero.heal(hero)
        if enemy.type is "ogre":    
            enemy = hero.findNearestEnemy()
            if hero.isReady("throw") and hero.distanceTo(enemy.pos) < hero.throwRange:
                    hero.throw(enemy)
            else:
                hero.attack(enemy)

            For some reason it doesn't work please help

P.S I am using a ranger

You do not need to redefine enemy after line 9.

1 Like

in addition to Hellenar’s point,
instead of:

if enemy.type is "ogre":

try:

if enemy.type == "ogre":

also, I think you need to put () after hero.throwRange
EDIT - Whoops, hero.throwRange doesn’t take ().

1 Like

I think that both is and == work the same.

is and == are actually different. is compares the two objects by reference and == compares their value. For example:

y = 99

if y == 99:
    hero.say("A")

if 99 is "9" + "9":
    hero.say("B")

Your hero will only say A because y equals to 99 as I defined in the line above. 99 is not “9” + “9” even though if you have your hero say (“9” + “9”), he will return 99 because you are combining to objects into one. So while they have the same facial value, they are two separate objects and changing one will object’s value will not affect the other.

Check out this stack overflow discussion if you are still confused:

https://stackoverflow.com/questions/132988/is-there-a-differen

1 Like