# Help codecombat hit and freeze

**URL:** <https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739>\
**Category:** Uncategorized\
**Created:** [June 17, 2017, 3:37pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739 "2017-06-17T15:37:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjamin\_van\_den\_Bra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/benjamin_van_den_bra/32/6241_2.png) [@Benjamin\_van\_den\_Bra](https://discourse.codecombat.com/u/Benjamin_van_den_Bra)\
**Post date:** [June 17, 2017, 3:37pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/1 "2017-06-17T15:37:37Z")

</div>

help me with the hit and freeze this is the code i use

# 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.
inAttackRange(enemy);
# If the result stored in canAttack is True, then attack!
hero.attack(enemy)
pass
```

---

<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 17, 2017, 4:15pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/2 "2017-06-17T16:15:07Z")

</div>

When the code says to call a function and save it in a variable, it should look something like this:

```python
exampleVariable = exampleFunction(parameter)

```

Then you use `if exampleVariable = True:` and do whatever you need to do. Use this format to correct the last few lines of your code, and you’re good to go.  
Hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![Benjamin\_van\_den\_Bra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/benjamin_van_den_bra/32/6241_2.png) [@Benjamin\_van\_den\_Bra](https://discourse.codecombat.com/u/Benjamin_van_den_Bra)\
**Post date:** [June 17, 2017, 5:31pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/3 "2017-06-17T17:31:30Z")

</div>

i don,t understand:slightly\_smiling\_face:

---

<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 17, 2017, 11:34pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/4 "2017-06-17T23:34:03Z")

</div>

So in your code, the level tells you to call inAttackRange(enemy), right? Then it tells you to save the result in the variable canAttack.  
Therefore if we replace the names of the variables, functions, and parameters:

```python
exampleVariable = exampleFunction(parameter)
if exampleVariable = True:
# turns into...
canAttack = inAttackRange(enemy)
if canAttack = True:

```

Hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![Benjamin\_van\_den\_Bra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/benjamin_van_den_bra/32/6241_2.png) [@Benjamin\_van\_den\_Bra](https://discourse.codecombat.com/u/Benjamin_van_den_Bra)\
**Post date:** [June 19, 2017, 3:20pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/5 "2017-06-19T15:20:34Z")

</div>

i,am not 1 of the pros so i don,t understand

---

<div class="post-metadata">

**Author:** ![Serg](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/serg/32/3260_2.png) [@Serg](https://discourse.codecombat.com/u/Serg)\
**Post date:** [June 19, 2017, 4:51pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/6 "2017-06-19T16:51:48Z")

</div>

It helps us help you if you format your code correctly. If you are going to ask for help on the forums please surround your code in triple backticks ( ` ) so we can see your indentation.

As for the help, there is a comment telling you what to do:

```python
# Call inAttackRange(enemy), with the enemy as the argument
# and save the result in the variable canAttack.

```

You are currently calling `inAttackRange(enemy)` but you are not `save the result in a variable`

---

<div class="post-metadata">

**Author:** ![Benjamin\_van\_den\_Bra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/benjamin_van_den_bra/32/6241_2.png) [@Benjamin\_van\_den\_Bra](https://discourse.codecombat.com/u/Benjamin_van_den_Bra)\
**Post date:** [June 19, 2017, 5:16pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/8 "2017-06-19T17:16:37Z")

</div>

‘’ # Call inAttackRange(enemy), with the enemy as the argument’’  
# and save the result in the variable canAttack.

---

<div class="post-metadata">

**Author:** ![Serg](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/serg/32/3260_2.png) [@Serg](https://discourse.codecombat.com/u/Serg)\
**Post date:** [June 19, 2017, 11:54pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/9 "2017-06-19T23:54:19Z")

</div>

Hello, you’re using `'` which isn’t ( ` ).

The ` key is near the 1 key, it might look like ~. Use 3 of the before and after your code.

---

<div class="post-metadata">

**Author:** ![Benjamin\_van\_den\_Bra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/benjamin_van_den_bra/32/6241_2.png) [@Benjamin\_van\_den\_Bra](https://discourse.codecombat.com/u/Benjamin_van_den_Bra)\
**Post date:** [June 22, 2017, 4:19pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/10 "2017-06-22T16:19:04Z")

</div>

`Call inAttackRange(enemy), with the enemy as the argument`  
`and save the result in the variable canAttack`

---

<div class="post-metadata">

**Author:** ![Serg](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/serg/32/3260_2.png) [@Serg](https://discourse.codecombat.com/u/Serg)\
**Post date:** [June 22, 2017, 5:08pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/11 "2017-06-22T17:08:30Z")

</div>

Maybe this will help?

 ![](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/4/449752bd894b569d7f106915bd38d2712ac65855.png)

---

<div class="post-metadata">

**Author:** ![Benjamin\_van\_den\_Bra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/benjamin_van_den_bra/32/6241_2.png) [@Benjamin\_van\_den\_Bra](https://discourse.codecombat.com/u/Benjamin_van_den_Bra)\
**Post date:** [June 22, 2017, 5:28pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/12 "2017-06-22T17:28:28Z")

</div>

that does,not work but please help me

---

<div class="post-metadata">

**Author:** ![Serg](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/serg/32/3260_2.png) [@Serg](https://discourse.codecombat.com/u/Serg)\
**Post date:** [June 22, 2017, 5:30pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/13 "2017-06-22T17:30:01Z")

</div>

We can’t help unless we can see your indentation. See the screenshot and add 3x ( ` ) before and after.

---

<div class="post-metadata">

**Author:** ![Benjamin\_van\_den\_Bra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/benjamin_van_den_bra/32/6241_2.png) [@Benjamin\_van\_den\_Bra](https://discourse.codecombat.com/u/Benjamin_van_den_Bra)\
**Post date:** [June 22, 2017, 5:33pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/14 "2017-06-22T17:33:52Z")

</div>

how do you make a screenshot serg

---

<div class="post-metadata">

**Author:** ![Serg](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/serg/32/3260_2.png) [@Serg](https://discourse.codecombat.com/u/Serg)\
**Post date:** [June 22, 2017, 5:34pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/15 "2017-06-22T17:34:19Z")

</div>

You don’t need to make a screenshot, you just need to add 3x backticks ( ` ) before and after your code.

![](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex016/uploads/codecombat/original/2X/2/24317776bcb1f1bf645c747e302dbacf41aa8951.png)

---

<div class="post-metadata">

**Author:** ![Benjamin\_van\_den\_Bra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/benjamin_van_den_bra/32/6241_2.png) [@Benjamin\_van\_den\_Bra](https://discourse.codecombat.com/u/Benjamin_van_den_Bra)\
**Post date:** [June 22, 2017, 5:36pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/16 "2017-06-22T17:36:09Z")

</div>

```python

# 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.
    
    # If the result stored in canAttack is True, then attack!
    
    pass

```

is this it

---

<div class="post-metadata">

**Author:** ![\_TD\_RodYT](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/_td_rodyt/32/6285_2.png) [@\_TD\_RodYT](https://discourse.codecombat.com/u/_TD_RodYT)\
**Post date:** [June 22, 2017, 6:50pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/17 "2017-06-22T18:50:45Z")

</div>

That is correct (20 chars)

---

<div class="post-metadata">

**Author:** ![\_TD\_RodYT](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/_td_rodyt/32/6285_2.png) [@\_TD\_RodYT](https://discourse.codecombat.com/u/_TD_RodYT)\
**Post date:** [June 22, 2017, 6:55pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/18 "2017-06-22T18:55:19Z")

</div>

I see you do not understand your mistake. Hopefully this will clarify.

Read the instructions inside the gray lines.

`# Call inAttackRange(enemy), with the enemy as the argument`  
`# and save the result in the variable canAttack.`

Use what you have learned in previous levels to solve this. First call in inAttackRange, as the gray line has said. The code for calling inAttackRange is right there. Then to the left of it, assign it to variable canAttack (something you learned in dungeon).

`# If the result stored in canAttack is True, then attack!`

After that, write an if statement saying if canAttack is true, attack.

---

<div class="post-metadata">

**Author:** ![Benjamin\_van\_den\_Bra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/benjamin_van_den_bra/32/6241_2.png) [@Benjamin\_van\_den\_Bra](https://discourse.codecombat.com/u/Benjamin_van_den_Bra)\
**Post date:** [June 23, 2017, 6:43pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/20 "2017-06-23T18:43:37Z")

</div>

i don,t understand  
`# Call inAttackRange(enemy), with the enemy as the argument`  
`# and save the result in the variable canAttack.`

---

<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 23, 2017, 6:48pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/21 "2017-06-23T18:48:43Z")

</div>

Remember what I mentioned earlier about saving a function’s result in a variable:

> [@Hellenar](#):
>
> exampleVariable = exampleFunction(parameter)  
> if exampleVariable = True:

---

<div class="post-metadata">

**Author:** ![Benjamin\_van\_den\_Bra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/benjamin_van_den_bra/32/6241_2.png) [@Benjamin\_van\_den\_Bra](https://discourse.codecombat.com/u/Benjamin_van_den_Bra)\
**Post date:** [June 24, 2017, 1:32pm UTC](https://discourse.codecombat.com/t/help-codecombat-hit-and-freeze/11739/22 "2017-06-24T13:32:51Z")

</div>

yes i now but i don,t understand  
the example part

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