[SOLVED]Ok for real I am really confused with the level Mind The Trap

Ok so I am confused, whenever I try to make the if (dist > 10): command work and it isn’t working, when I do the correction it wants me to it fails with the Ran Out of Time failure text; if anyone wants to see my code here it is:

# If you try to attack a distant enemy, your hero will charge toward it, ignoring all flags.
# You'll need to make sure you only attack enemies who are close to you!

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 (dist < 10):
            hero.attack(enemy)

and I can attach a screenshot if anyone needs me to as well I can attatch it later on…

Hello, @LunAero_Gaming and welcome to the Forum! :partying_face:

Maybe:
if hero.distanceTo(enemy) <10:?

@LunAero_Gaming you have to be specific with your code you shouldn’t use words that aren’t a tactic

Have you defined dist yet? You have to define dist first in order for your code to work.
So you would do hero.distanceTo(enemy) in the very beginning of your while-True loop.
Or you could do it the way PeterPalov did it.
Lydia

Yeah, or you can do:

dist = hero.distanceTo(enemy)
if dist < 10:

ok so I am trying these right now I have yet to try it but here is my current code: (also sorry for late reply)

# If you try to attack a distant enemy, your hero will charge toward it, ignoring all flags.
# You'll need to make sure you only attack enemies who are close to you!

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
        dist = hero.distanceTo(enemy)
        if dist < 10

I will try these after another reply since I am really busy lately
also edit: just noticed there is an error


not sure how to fix it

You need to add a colon at the end of an if statement, so
if dist < 10
would be
if dist < 10:

Add a colon at the end
You also need to add an attack inside this if statement.
Lydia

thx will try now, this is been pretty challenging

Please remove the working code as it is against the Discourse rules.
And congrats on solving the level!
Lydia

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.