Hit and freeze help please

heres my code

def inAttackRange(enemy):
    distance = hero.distanceTo(enemy)
# Almost all swords have attack range of 3.
if hero.distanceTo(enemy) <= 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
    # and save the result in the variable canAttack.
    inAttackRange(enemy);
    # If the result stored in canAttack is True, then attack!
    hero.attack(enemy)
pass

it doesn’t work can someone help

1 Like

Hey there, @Amber,

I noticed your spacing is off. Is that just how it copied?

In the lines

def inAttackRange(enemy):
    distance = hero.distanceTo(enemy)
    # Almost all swords have attack range of 3.
    if hero.distanceTo(enemy) <= 3:
        return True
    else:
        return False

you already defined hero.distanceTo(enemy) as distance. Use the variable instead.

use the variable canAttack in your while loop to save the function created and then use an if to see if the function is true. if it is true, then attack the enemy.

Hope that helps,
-@Anna

1 Like

Not that it matters for functioning code, the same goes with the final ‘pass’ statement. Sure it is just a placeholder, but it is best practice to place it correctly so that you don’t get in to the habit and forget in future code.

3 Likes

Here check if what inAttackRange(enemy) returns is true and if so, then attack the enemy.

Andrei

3 Likes

Which campaign is this puzzle in?

Forest: https://codecombat.com/play/level/hit-and-freeze

1 Like

Right then. I can help now.

To check if something is true, you might want to think about this:

if canEat is True:
    hero.eat(cookie)

Ahh okay that makes sense now I will have a look in a few thank you everyone for the help

2 Likes

Happy to help as helping is happiness.

2 Likes

So, is this topic solved now?

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.

not quite it says that

if hero.distanceTo(enemy) is <= 3:

is wrong and I don’t know what to do
it says argument error for line four.

def inAttackRange(enemy):
    distance = hero.distanceTo(enemy)
# Almost all swords have attack range of 3.
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

my new code is also above so you can check it out. if anything else is wrong let me know!

Here try to put something like this:

make a new variabile called canAttack and give it the boolean value that inAttackRange(enemy) returns
check if what is stored in canAttack is True
    attack the enemy

Andrei

1 Like

ill try it and let you know.

1 Like

And put this inside the inAttackRange function.

Andrei

Remember to do this to attack/eat the cookie/enemy.

1 Like
def inAttackRange(enemy):
    distance = hero.distanceTo(enemy)
# Almost all swords have attack range of 3.
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 code

says thats wrong
i cant fix it