# 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:** 19\
**Page:** 2

<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 17, 2020, 9:45am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/21 "2020-07-17T09:45:24Z")

</div>

> [@Amber](#):
>
> `enemy = canAttack`

You already have the enemy found, don’t you think?

> [@Amber](#):
>
> `inAttackRange(enemy);`

Here check and only if inAttackRange(enemy) returns true attack the enemy.

Andrei

---

<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 17, 2020, 9:46am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/22 "2020-07-17T09:46:49Z")

</div>

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

And put this inside the inAttackRange function (by putting 4 spaces before those lines).

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 17, 2020, 9:49am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/23 "2020-07-17T09:49:10Z")

</div>

```python
def inAttackRange(enemy):
    distance = hero.distanceTo(enemy)
    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 newest code i did what u said but it still says its wrong

---

<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:49am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/24 "2020-07-17T09:49:52Z")

</div>

![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/0/0de655846f975259f6f3f30b659d859aa8370c53.jpeg)

---

<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:51am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/25 "2020-07-17T09:51:26Z")

</div>

![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/2/2aea3c3d1e8e6b6f6af886aff0a75745079d9436.jpeg)

---

<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 17, 2020, 9:54am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/26 "2020-07-17T09:54:26Z")

</div>

I told you to also put 4 spaces also before this

> [@Amber](#):
>
> ```python
> return True
> else:
> return False
> 
> ```

And you did not correct this

> [@AnSeDra](#):
>
> Here check and only if inAttackRange(enemy) returns 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 17, 2020, 9:54am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/27 "2020-07-17T09:54:54Z")

</div>

ooop sorry give me a min

---

<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, 10:00am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/28 "2020-07-17T10:00:54Z")

</div>

```python
def inAttackRange(enemy):
    distance = hero.distanceTo(enemy)
    if inAttackRange(enemy):
        return True
        hero.attack(enemy)
        
    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

```

line 13 is wrong but i don’t know how to solve it

---

<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, 10:02am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/29 "2020-07-17T10:02:27Z")

</div>

> [@Amber](#):
>
> `enemy = canAttack`

it says this one is wrong. how do i solve it?

---

<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, 10:08am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/30 "2020-07-17T10:08:56Z")

</div>

![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a6906cea8393c8fca3da1eedf33ec2f79470adcb.jpeg)

this what it ways

---

<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 17, 2020, 10:19am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/31 "2020-07-17T10:19:30Z")

</div>

> [@Hit and freeze help please](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/21):
>
> You already have the enemy found, don’t you think? Here check and only if inAttackRange(enemy) returns true attack the enemy. Andrei

You still did not correct this I see.

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 17, 2020, 11:17am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/32 "2020-07-17T11:17:22Z")

</div>

so i dun that and here my code

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

```

but it still dont work it just stops midway

---

<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, 11:17am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/33 "2020-07-17T11:17:49Z")

</div>

![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/8/8c3f6efbf678a8324f2fe8a9dee36801d12e1b72.jpeg)

---

<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 17, 2020, 11:22am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/34 "2020-07-17T11:22:33Z")

</div>

> [@Amber](#):
>
> `if inAttackRange(enemy):`

This one should be instead of all of this

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

And also not use the else anymore. Inside the if statement attack the enemy. and return true out of the loop only if hero.distanceTo(enemy) \<= 3.

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 17, 2020, 11:29am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/35 "2020-07-17T11:29:58Z")

</div>

im confused  
what do i get rid of?

---

<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 17, 2020, 11:36am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/36 "2020-07-17T11:36:42Z")

</div>

Delete this

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

Put this

> [@Amber](#):
>
> `if inAttackRange(enemy):`

after this

> [@Amber](#):
>
> `# and save the result in the variable canAttack.`

Inside of that if that you just moved attack the enemy.  
In the place from where you removed the if make another if that checks if the distance between the hero and the enemy is less or equal to 3.

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:** [October 18, 2020, 10:02am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/37 "2020-10-18T10:02:36Z")

</div>

hi its been a while ive been a bit busy ive been trying to complete this level and it wont let me ive tried reprogramming it but it just makes all the trolls wander around then after 30 seconds they attack me and i die.  
heres my code but it doesnt tell me what ive done wrong.

```python
# You are trapped. Don't move, it'll be painful.

# This function checks if the enemy is in your attack range.
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.
    enemy - hero.findNearestEnemy()
    # Call inAttackRange(enemy), with the enemy as the argument
    # and save the result in the variable canAttack.
    canAttack - inAttackRange(enemy)
    # If the result stored in canAttack is True, then attack!
    if canAttack true:
        hero.attack(enemy)
    pass

```

---

<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:** [October 18, 2020, 3:09pm UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/38 "2020-10-18T15:09:40Z")

</div>

Hi @Amber.  
Firstly a syntax problem:  
You’ve used - to define the variables “enemy” and “canAttack”. That is incorrect, - is a mathematical operator meaning minus. You should use = (like you have in your previous code ⬆), that means “equals”, or “is”.

> [@Amber](#):
>
> `if canAttack true:`

Just another technical problem. You don’t say “if something True”, you say “if something == True”. (note the T in True is capitalised).  
If you want an even simpler solution you can say:

```python
if canAttack:

```

Now, why is that?  
It’s because canAttack is a “boolean” variable. That means it’s True or False. (remember when you returned True or False in your function?)  
You can use “if” to check if a boolean variable is set to True.

```python
boolean = True
if boolean:
    hero.say("True!")
else:
    hero.say("False...")

```

If boolean was True (like in the example) the hero will say “True!”, and if the boolean was False, the hero would say “False…”.  
I hope that helps you understand it.  
Danny

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [October 19, 2020, 1:09am UTC](https://discourse.codecombat.com/t/hit-and-freeze-help-please/22840/39 "2020-10-19T01:09:03Z")

</div>

> [@Amber](#):
>
> `-`

Pretty sure you meant `=` right?  
Lydia

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