# Hit and freeze help please

**URL:** <https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840>\
**Category:** Level Help\
**Created:** [July 7, 2020, 1:50pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840 "2020-07-07T13:50:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amber](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/amber/32/13524_2.png) [@Amber](https://discourse.codecombat.com/u/Amber)\
**Post date:** [July 7, 2020, 1:50pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/1 "2020-07-07T13:50:09Z")

</div>

heres my code

```python
def inAttackRange(enemy):
    distance = hero.distanceTo(enemy)
# Almost all swords have attack range of 3.
if hero.distanceTo(enemy) <= 3:
    return True
else:
    return False
while True:
    # Find the nearest enemy and store it in a variable.
    enemy = hero.findNearestEnemy()
    # Call inAttackRange(enemy), with the enemy as the argument
    # and save the result in the variable canAttack.
    inAttackRange(enemy);
    # If the result stored in canAttack is True, then attack!
    hero.attack(enemy)
pass

```

it doesn’t work can someone help

---

<div class="post-metadata">

**Author:** ![Anna](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/anna/32/8378_2.png) [@Anna](https://discourse.codecombat.com/u/Anna)\
**Post date:** [July 7, 2020, 1:56pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/2 "2020-07-07T13:56:52Z")

</div>

Hey there, @Amber,

I noticed your spacing is off. Is that just how it copied?

In the lines

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

```

you already defined `hero.distanceTo(enemy)` as `distance`. Use the variable instead.

use the variable `canAttack` in your while loop to save the function created and then use an `if` to see if the function is true. if it is true, then attack the enemy.

Hope that helps,  
-@Anna

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [July 7, 2020, 2:05pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/3 "2020-07-07T14:05:15Z")

</div>

Not that it matters for functioning code, the same goes with the final ‘pass’ statement. Sure it is just a placeholder, but it is best practice to place it correctly so that you don’t get in to the habit and forget in future code.

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [July 7, 2020, 2:36pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/4 "2020-07-07T14:36:38Z")

</div>

> [@Amber](#):
>
> ```python
> inAttackRange(enemy);
> # If the result stored in canAttack is True, then attack!
> hero.attack(enemy)
> 
> ```

Here check if what inAttackRange(enemy) returns is true and if so, then attack the enemy.

Andrei

---

<div class="post-metadata">

**Author:** ![FalconX11\_312](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/falconx11_312/32/26330_2.png) [@FalconX11\_312](https://discourse.codecombat.com/u/FalconX11_312)\
**Post date:** [July 7, 2020, 6:46pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/5 "2020-07-07T18:46:14Z")

</div>

Which campaign is this puzzle in?

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [July 7, 2020, 6:48pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/6 "2020-07-07T18:48:15Z")

</div>

Forest: [https://codecombat.com/play/level/hit-and-freeze](https://codecombat.com/play/level/hit-and-freeze)

---

<div class="post-metadata">

**Author:** ![FalconX11\_312](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/falconx11_312/32/26330_2.png) [@FalconX11\_312](https://discourse.codecombat.com/u/FalconX11_312)\
**Post date:** [July 7, 2020, 6:52pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/7 "2020-07-07T18:52:40Z")

</div>

Right then. I can help now.

---

<div class="post-metadata">

**Author:** ![FalconX11\_312](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/falconx11_312/32/26330_2.png) [@FalconX11\_312](https://discourse.codecombat.com/u/FalconX11_312)\
**Post date:** [July 7, 2020, 7:00pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/8 "2020-07-07T19:00:31Z")

</div>

To check if something is true, you might want to think about this:

```python
if canEat is True:
    hero.eat(cookie)

```

---

<div class="post-metadata">

**Author:** ![Amber](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/amber/32/13524_2.png) [@Amber](https://discourse.codecombat.com/u/Amber)\
**Post date:** [July 7, 2020, 7:01pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/9 "2020-07-07T19:01:59Z")

</div>

Ahh okay that makes sense now I will have a look in a few thank you everyone for the help

---

<div class="post-metadata">

**Author:** ![FalconX11\_312](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/falconx11_312/32/26330_2.png) [@FalconX11\_312](https://discourse.codecombat.com/u/FalconX11_312)\
**Post date:** [July 7, 2020, 7:03pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/10 "2020-07-07T19:03:22Z")

</div>

Happy to help as helping is happiness.

---

<div class="post-metadata">

**Author:** ![FalconX11\_312](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/falconx11_312/32/26330_2.png) [@FalconX11\_312](https://discourse.codecombat.com/u/FalconX11_312)\
**Post date:** [July 7, 2020, 7:17pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/11 "2020-07-07T19:17:16Z")

</div>

So, is this topic solved now?

---

<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:** [July 8, 2020, 7:17am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/12 "2020-07-08T07:17:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [July 8, 2020, 3:34pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/13 "2020-07-08T15:34:56Z")

</div>



---

<div class="post-metadata">

**Author:** ![Amber](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/amber/32/13524_2.png) [@Amber](https://discourse.codecombat.com/u/Amber)\
**Post date:** [July 13, 2020, 10:46am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/14 "2020-07-13T10:46:27Z")

</div>

not quite it says that

```python
if hero.distanceTo(enemy) is <= 3:

```

is wrong and I don’t know what to do  
it says argument error for line four.

```python
def inAttackRange(enemy):
    distance = hero.distanceTo(enemy)
# Almost all swords have attack range of 3.
if hero.distanceTo(enemy) is <= 3:
    return True
else:
    return False
while True:
    # Find the nearest enemy and store it in a variable.
    enemy = hero.findNearestEnemy()
    # Call inAttackRange(enemy), with the enemy as the argument
    enemy = canAttack
    # and save the result in the variable canAttack.
    inAttackRange(enemy);
    # If the result stored in canAttack is True, then attack!
    hero.attack(enemy)
pass

```

my new code is also above so you can check it out. if anything else is wrong let me know!

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [July 13, 2020, 10:50am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/15 "2020-07-13T10:50:41Z")

</div>

> [@Amber](#):
>
> ```python
> enemy = canAttack
> # and save the result in the variable canAttack.
> inAttackRange(enemy);
> # If the result stored in canAttack is True, then attack!
> hero.attack(enemy)
> 
> ```

Here try to put something like this:

```python
make a new variabile called canAttack and give it the boolean value that inAttackRange(enemy) returns
check if what is stored in canAttack is True
    attack the enemy

```

Andrei

---

<div class="post-metadata">

**Author:** ![Amber](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/amber/32/13524_2.png) [@Amber](https://discourse.codecombat.com/u/Amber)\
**Post date:** [July 13, 2020, 10:51am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/16 "2020-07-13T10:51:52Z")

</div>

ill try it and let you know.

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [July 13, 2020, 10:53am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/17 "2020-07-13T10:53:59Z")

</div>

> [@Amber](#):
>
> ```python
> if hero.distanceTo(enemy) is <= 3:
> return True
> else:
> return False
> 
> ```

And put this inside the inAttackRange function.

Andrei

---

<div class="post-metadata">

**Author:** ![FalconX11\_312](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/falconx11_312/32/26330_2.png) [@FalconX11\_312](https://discourse.codecombat.com/u/FalconX11_312)\
**Post date:** [July 13, 2020, 1:25pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/18 "2020-07-13T13:25:36Z")

</div>

> [@FalconX11\_312](#):
>
> To check if something is true, you might want to think about this:
> 
> ```python
> if canEat is True:
> hero.eat(cookie)
> 
> ```

Remember to do this to attack/eat the cookie/enemy.

---

<div class="post-metadata">

**Author:** ![Amber](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/amber/32/13524_2.png) [@Amber](https://discourse.codecombat.com/u/Amber)\
**Post date:** [July 17, 2020, 9:40am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/19 "2020-07-17T09:40:00Z")

</div>

```python
def inAttackRange(enemy):
    distance = hero.distanceTo(enemy)
# Almost all swords have attack range of 3.
if hero.distanceTo(enemy) is <= 3:
    return True
else:
    return False
while True:
    # Find the nearest enemy and store it in a variable.
    enemy = hero.findNearestEnemy()
    # Call inAttackRange(enemy), with the enemy as the argument
    enemy = canAttack
    # and save the result in the variable canAttack.
    inAttackRange(enemy);
    # If the result stored in canAttack is True, then attack!
    hero.attack(enemy)
pass

```

thats my code

---

<div class="post-metadata">

**Author:** ![Amber](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/amber/32/13524_2.png) [@Amber](https://discourse.codecombat.com/u/Amber)\
**Post date:** [July 17, 2020, 9:40am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/20 "2020-07-17T09:40:40Z")

</div>

> [@Amber](#):
>
> `if hero.distanceTo(enemy) is <= 3:`

says thats wrong  
i cant fix it

[Next page](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840.md?page=2)
