Hit and Freeze (Lvl 16)

I don’t really get how to solve this level, can anybody help me?

Heres my code

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 <= 2:
return True
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 hero == inAttackRange(enemy):
    return True
# If the result stored in canAttack is True, then attack!
if hero == canAttack == True:
    canAttack = inAttackRange(enemy)

Welcome the CoCo Discourse forums @maidenabyss ! :partying_face:
This is a place to get help within levels, dicuss ideas, give suggestions for the game or just chat with other awesome coders!
Check out the FAQ and the rules if you haven’t already and have fun! :grinning_face_with_smiling_eyes:

Please make sure you post your code correctly by pressing image


hey, there do you mind putting all of your code properly inside of the space provided after you hit the </> button and then we will be happy to help thanks.


change <=2 to <3 (20 chars)

1 Like

instead of this

if hero == inAttackRange(enemy):

use this

canAttack = inAttackRange(enemy)
if canAttack == True

and delete if hero == canAttack == True:
remember to define your variables BEFORE you try to use them, because they will not exist before their creation.

1 Like