# Hit and freeze help please

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

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

Andrei

2 Likes

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

Andrei

2 Likes
``````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

1 Like

I told you to also put 4 spaces also before this

And you did not correct this

Andrei

1 Like

ooop sorry give me a min

1 Like
``````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

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

this what it ways

You still did not correct this I see.

Andrei

1 Like

so i dun that and here my code

``````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

This one should be instead of all of this

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

1 Like

im confused
what do i get rid of?

Delete this

Put this

after this

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

3 Likes

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.

``````# 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
``````

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”.

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:

``````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.

``````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

Pretty sure you meant `=` right?
Lydia