Help codecombat hit and freeze

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

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

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 :slightly_smiling_face:

1 Like

i don,t understand:slightly_smiling_face:

1 Like

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:

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

Hope this helps :slight_smile:

1 Like

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

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:

# 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

2 Likes

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

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.

2 Likes

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

Maybe this will help?

2 Likes

that does,not work but please help me

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

2 Likes

how do you make a screenshot serg

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

2 Likes

# 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

That is correct (20 chars)

1 Like

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.

2 Likes

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

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

1 Like

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