# \`hit and freeze

**URL:** <https://discourse.codecombat.com/t/hit-and-freeze/29193>\
**Category:** Level Help\
**Created:** [June 3, 2021, 2:41pm UTC](https://discourse.codecombat.com/t/hit-and-freeze/29193 "2021-06-03T14:41:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashton\_Gifford](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ashton_gifford/32/24614_2.png) [@Ashton\_Gifford](https://discourse.codecombat.com/u/Ashton_Gifford)\
**Post date:** [June 3, 2021, 2:41pm UTC](https://discourse.codecombat.com/t/hit-and-freeze/29193/1 "2021-06-03T14:41:30Z")

</div>

im on hit and freeze i cant figure it out. pls help. heres my code.```

```python
def inAttackRange(enemy):
    distance = hero.distanceTo(enemy)
    # Almost all swords have attack range of 3.
    if distance <= 3:
        return True
    else:
        return False

# Attack ogres only when they're within reach.
while True:
    # Find the nearest enemy and store it in a variable.
    canAttack = hero.findNearestEnemy()
    # Call inAttackRange(enemy), with the enemy as the argument
    # and save the result in the variable canAttack.
    canAttack = inAttackRange(canAttack)
    # If the result stored in canAttack is True, then attack!
    if canAttack == true:
        hero.cleave(enemy)
    pass

```

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [June 3, 2021, 5:15pm UTC](https://discourse.codecombat.com/t/hit-and-freeze/29193/2 "2021-06-03T17:15:01Z")

</div>

> [@Ashton\_Gifford](#):
>
> ` canAttack = hero.findNearestEnemy()`

Change that first canAttack variable to enemy.

> [@Ashton\_Gifford](#):
>
> `inAttackRange(canAttack)`

Then change this canAttack parameter to enemy.

> [@Ashton\_Gifford](#):
>
> ` if canAttack == true:`

Then just check if cannAttack:

```python
if canAttack:
    #attack not cleave

```

---

<div class="post-metadata">

**Author:** ![Ashton\_Gifford](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ashton_gifford/32/24614_2.png) [@Ashton\_Gifford](https://discourse.codecombat.com/u/Ashton_Gifford)\
**Post date:** [June 3, 2021, 5:23pm UTC](https://discourse.codecombat.com/t/hit-and-freeze/29193/3 "2021-06-03T17:23:21Z")

</div>

thank you. it worked

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [June 4, 2021, 5:23am UTC](https://discourse.codecombat.com/t/hit-and-freeze/29193/4 "2021-06-04T05:23:59Z")

</div>

Closed on June 4, 2021, 5:23am UTC

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