[Solved] Mind the Trap Python Help

This is my code:

while True:
    flag = hero.findFlag()
    enemy = hero.findNearestEnemy()
    if flag:
        # Pick up the flag.
        hero.pickUpFlag(flag)
        hero.say("I should pick up the flag.")
    elif enemy:
        # Only attack if the enemy distance is < 10 meters
        if enemy < 10:
            hero.attack(enemy)

But for some reason, my hero won’t attack. Please help!

Can you take a screenshot of what is happening @Garv_Jain

1 Like

I can’t but what happens is that when the ogres attack my hero, he won’t attack back

Lets see :thinking:. Are you placing flags so that your hero can go attack them?

1 Like

like this:

1 Like

Yes, and my hero will move to the flag, just won’t attack

Hmm… That is weird i am using the same code as you

1 Like

AHA! you didn’t find the distance to your enemy

1 Like

So then what code do I need to add

You have to find the distance to your enemy then say if distance <10 not if enemy<10 because you have to find the distance

1 Like
distance = hero.distanceTo(enemy)
1 Like

Than say if distance <10

1 Like

Ohhhh. Thanks. That makes a lot more sense

No problem. Im happy to help any time

1 Like

And after you have solved the problem could you please put Solved on the title because it is a pain for me to always put solved for every one @Garv_Jain

2 Likes

For your next one you should put solved but i will do this one. Just this one. See you around @Garv_Jain :smile:. Good Job!

2 Likes

don’t post your solution, Enderlord832. Just don’t.

I didn’t. I was just helping him

1 Like

You posted your solution by using the screenshot. next time crop the image.

Oh ok. Next time i will do that

1 Like