# Mind the Trap Python

**URL:** <https://discourse.codecombat.com/t/mind-the-trap-python/11818>\
**Category:** Uncategorized\
**Created:** [June 27, 2017, 7:40pm UTC](https://discourse.codecombat.com/t/mind-the-trap-python/11818 "2017-06-27T19:40:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![earthy415](https://avatars.discourse-cdn.com/v4/letter/e/50afbb/32.png) [@earthy415](https://discourse.codecombat.com/u/earthy415)\
**Post date:** [June 27, 2017, 7:40pm UTC](https://discourse.codecombat.com/t/mind-the-trap-python/11818/1 "2017-06-27T19:40:47Z")

</div>

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

```

I’m not sure what I’m doing wrong. Hero won’t attack.

---

<div class="post-metadata">

**Author:** ![Seojin\_Roy\_Lee](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/seojin_roy_lee/32/23539_2.png) [@Seojin\_Roy\_Lee](https://discourse.codecombat.com/u/Seojin_Roy_Lee)\
**Post date:** [June 27, 2017, 7:53pm UTC](https://discourse.codecombat.com/t/mind-the-trap-python/11818/2 "2017-06-27T19:53:35Z")

</div>

If you’re trying to look for the distance between you and the enemy, you have to put down `hero.distanceTo(enemy)` in order for the computer to understand the distance between you and the enemy

---

<div class="post-metadata">

**Author:** ![Hellenar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hellenar/32/9100_2.png) [@Hellenar](https://discourse.codecombat.com/u/Hellenar)\
**Post date:** [June 27, 2017, 7:54pm UTC](https://discourse.codecombat.com/t/mind-the-trap-python/11818/3 "2017-06-27T19:54:28Z")

</div>

What is `if enemy < 10:`? To check a unit’s distance from the hero, you can use a method called `hero.distanceTo(unit)`. In this case, the unit is the enemy.

---

<div class="post-metadata">

**Author:** ![Hellenar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hellenar/32/9100_2.png) [@Hellenar](https://discourse.codecombat.com/u/Hellenar)\
**Post date:** [June 27, 2017, 7:54pm UTC](https://discourse.codecombat.com/t/mind-the-trap-python/11818/4 "2017-06-27T19:54:58Z")

</div>

Lol I literally responded at the same time as you 😂

---

<div class="post-metadata">

**Author:** ![Seojin\_Roy\_Lee](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/seojin_roy_lee/32/23539_2.png) [@Seojin\_Roy\_Lee](https://discourse.codecombat.com/u/Seojin_Roy_Lee)\
**Post date:** [June 27, 2017, 7:56pm UTC](https://discourse.codecombat.com/t/mind-the-trap-python/11818/5 "2017-06-27T19:56:23Z")

</div>

Oi! ( 20 characters)

---

<div class="post-metadata">

**Author:** ![Tanmay\_Kulkarni](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/tanmay_kulkarni/32/7360_2.png) [@Tanmay\_Kulkarni](https://discourse.codecombat.com/u/Tanmay_Kulkarni)\
**Post date:** [January 21, 2018, 7:06pm UTC](https://discourse.codecombat.com/t/mind-the-trap-python/11818/6 "2018-01-21T19:06:29Z")

</div>

```python
# 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 hero.distanceTo(enemy) < 10:
            hero.attack(enemy)

```

please help

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [January 21, 2018, 9:21pm UTC](https://discourse.codecombat.com/t/mind-the-trap-python/11818/7 "2018-01-21T21:21:37Z")

</div>

I have a few questions because the code is running but not completing. Please describe what is happening when you run your code. Also:

1. What glasses are you using?
2. What is your total health at the start?
3. What sword are you using?

---

<div class="post-metadata">

**Author:** ![Seojin\_Roy\_Lee](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/seojin_roy_lee/32/23539_2.png) [@Seojin\_Roy\_Lee](https://discourse.codecombat.com/u/Seojin_Roy_Lee)\
**Post date:** [February 20, 2018, 4:34pm UTC](https://discourse.codecombat.com/t/mind-the-trap-python/11818/8 "2018-02-20T16:34:30Z")

</div>

don’t say “I should pick up the flag” because that’s going to give your hero a 1 second delay.

---

<div class="post-metadata">

**Author:** ![Tin\_Lap\_CHU\_066.3](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/tin_lap_chu_066.3/32/10337_2.png) [@Tin\_Lap\_CHU\_066.3](https://discourse.codecombat.com/u/Tin_Lap_CHU_066.3)\
**Post date:** [December 27, 2018, 4:07am UTC](https://discourse.codecombat.com/t/mind-the-trap-python/11818/9 "2018-12-27T04:07:57Z")

</div>

```python
while True:
    flag = hero.findFlag()
    enemy = hero.findNearestEnemy()
    Distance = hero.distanceTo(enemy)
    if flag:
        # Pick up the flag.
        hero.pickUpFlag(flag)
    elif enemyDistance < 10:
        # Only attack if the enemy distance is < 10 meters
        hero.attack(enemy)
        hero.attack(enemy)

```

Whats wrong with my code?

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [December 27, 2018, 5:35am UTC](https://discourse.codecombat.com/t/mind-the-trap-python/11818/10 "2018-12-27T05:35:35Z")

</div>

> [@Tin\_Lap\_CHU\_066.3](#):
>
> Distance = hero.distanceTo(enemy) if flag: # Pick up the flag. hero.pickUpFlag(flag) elif enemyDistance \< 10:

Different names for variables. Change the `enemyDistance` to `Distance`

---

<div class="post-metadata">

**Author:** ![Pablo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/pablo/32/8824_2.png) [@Pablo](https://discourse.codecombat.com/u/Pablo)\
**Post date:** [January 21, 2019, 1:54pm UTC](https://discourse.codecombat.com/t/mind-the-trap-python/11818/11 "2019-01-21T13:54:36Z")

</div>

У меня он убил одну сторону врагов а других не видит и они не подходят ,вследовательно герой стоит на месте и ничего ни делает…

```python
while True:
    flag = hero.findFlag()
    enemy = hero.findNearestEnemy()
    if flag:
        # Возьми флаг.
        hero.pickUpFlag(flag)
    elif enemy:
        # Атакуй врага, если он находится на расстоянии < 10 метров
        distance = hero.distanceTo(enemy)
    if distance < 10:
        hero.attack(enemy)

```

Чего то тут не хватает ,подскажите чего?
