# Hit and freeze HELP

@rocketoofer, you need to use an if statement to check if the enemy exists in the first place. Something like this:

``````if enemy:
#Find the distance of the enemy
``````

Grzmot

Thank you. That was very helpful.

# This function checks if the enemy is in your attack range.

def inAttackRange(enemy):
distance = hero.distanceTo(enemy)
if distance <= 3:
return True
else:
return False

while True:
enemy = hero.findEnemies()

``````inAttackRange
canAttack = inAttackRange

if canAttack == True:
hero.attack(enemy)
pass
``````

What am I doing wrong… my dude keeps on getting killed and when it doesn’t move it doesn’t attack.

Hi Reyansh_Tandon,

Your function inAttackRange needs to have something to work on (called an argument) that you pass in - in this case (enemy).

So when you put ‘canAttack = inAttackRange’, this should be 'canAttack = inAttackRange(enemy). However, at the moment your function will only run if a single thing is put in (ie one enemy), but you’ve defined enemy as an array (‘enemy = findEnemies()’). What do you need to do to find one enemy, to put into the function?

Jenny

I need help with my code here it is:
It says there is no enemy

``````# You are trapped. Don't move, it'll be painful.
hero.findNearestEnemy()
# 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.
hero.findNearestEnemy()
# Call inAttackRange(enemy), with the enemy as the argument
# and save the result in the variable canAttack.
canAttack = inAttackRange()
# If the result stored in canAttack is True, then attack!
if canAttack == True:
hero.attack(enemy)
pass

``````

Delete that `hero.findNearestEnemy()`.

Change that to `enemy = hero.findNearestEnemy()`. Then indent all of the code after the while true loop so that it runs.

The reason `enemy` isn’t defined is because you didn’t store `hero.findNearestEnemy()` into a variable.
You didn’t define `enemy` because all you did was `hero.findNearestEnemy()`, you didn’t make `hero.findNearestEnemy()` a variable.

Same problem here. You didn’t store `hero.findNearestEnemy()` in a variable.
You only made the hero look for an enemy, but you didn’t keep track of the enemy.

Name the variable `enemy`. I can’t tell you how to make a variable or that’d be giving the answer

I think if you stored all of the `hero.findNearestEnemy()` into variable(s), I think you should be able to pass the level.

On line 18 it said that my code “InAttackrange” is not defined

``````# 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:
Hero.attack(enemy)
pass

``````

Note the * is because it is bolded

Capitalize the A and R, it should be `inAttackRange(enemy)`

Now it states that line 20 is not defined

Try making the `h` in `Hero.attack(enemy)` lowercase. (line 21)

Line 20 is still undefined

Make that:

``````if canAttack:
``````

Delete the enemy with the parenthesis. Also delete the string, you don’t need the " " around `canAttack = inAttackRange(enemy)`

Like @abc said, try make `if canAttack(enemy):` into `if canAttack:`.
I’m sorry if I’m mistaken, but I think `if canAttack:` should also be made into `if canAttack == True:`.

Thank you. I passed the level.

1 Like

NOOO I DELETED MY POST xD

Hey, so I tried that code and fixing it, but I get an error saying the `canAttack` isn’t defined. What’s wrong there?

Hi Necrotis1,

Welcome to the forum!

You’re posting on a topic that’s a few months old, so the others may not see it. If you’re stuck on the level, can you post your code (format it by clicking on the </> symbol above) and we’ll try to help you.

Jenny