Help codecombat hit and freeze


#1

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

#2

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:


#3

i don,t understand:slightly_smiling_face:


#4

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:


#5

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


#6

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


#8

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


#9

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.


#10

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


#11

Maybe this will help?


#12

that does,not work but please help me


#13

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


#14

how do you make a screenshot serg


#15

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


#16

# 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


#17

That is correct (20 chars)


#18

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.


#20

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


#21

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


#22

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