Hit and freeze HELP

I need help with this level what is wrong with 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 <= 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()
# If the result stored in canAttack is True, then attack!
if canAttack:
    hero.attack(enemy)
pass
3 Likes

define canAttack as True

Could you please format your code using </> symbol.

2 Likes

Again?

if canAttack == True:
    hero.attack(enemy)
pass

The whole code.
(20 chars)

1 Like

@Wolfhunter0

Please learn to post your code properly from the game from now on and use the </> button or it won’t format properly. When you click the </> button, the following will appear:

PostCode

Please paste ALL of your code inside the triple back tick marks. There are many people here willing and able to help. If you use the </> button correctly, your code should look like this:

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
    else:
        hero.say("My code is formatted properly")

If ALL of your code doesn’t look like the code above, then you’re doing it wrong and we can’t see the structure of the code to troubleshoot whether or not that is the issue. Use the </> button and help us help you. This works when sending a private message as well. Thank you.

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

to

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

@Seojin_Roy_Lee My last comment wasn’t directed at you. I know you know how to properly post code. I was explaining to @Wolfhunter0 how to do it.

O, ok (20 characters)

Thanks! (20 characters)

Hello, first time asking for help, could you tell what is wrong with my code.
Why is not working?
</>
// You are trapped. Don’t move, it’ll be painful.

// This function checks if the enemy is in your attack range.
function inAttackRange(enemy)
var 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.
var enemy = hero.findNearestEnemy();
// Call inAttackRange(enemy), with the enemy as the argument
// and save the result in the variable canAttack.
var inAttackRange;
// If the result stored in canAttack is true, then attack!
if (enemy === true)
hero.attack(inAttackRange);

</>
I do this because I don’t copy the code of some else, I want to learn. and enjoy do it. What happen whit my code.

you have to type down ``` twice, once in the top and once in the bottom. You replaced that for </>.

1 Like
// You are trapped. Don’t move, it’ll be painful.

// This function checks if the enemy is in your attack range.
function inAttackRange(enemy)
    var 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.
    var enemy = hero.findNearestEnemy();
    // Call inAttackRange(enemy), with the enemy as the argument
    // and save the result in the variable canAttack.
    var inAttackRange;
    // If the result stored in canAttack is true, then attack!
    if (enemy === true)
        hero.attack(inAttackRange);

change this:

if (enemy ==== true)
    hero.attack(inAttackRange)

to

if (enemy)
    if (canAttack(enemy) == True)
        hero.attack(enemy);

Forgive me if I’m wrong with this. You have to also define canAttack(). I’m only good with python and partially good at javascript, but this looks like coffeescript to me, so it’s hard to help.

Hello, thanks for your commentary. Is not a coffee script is javascript I am learning.
I am triyin your little chain.

// You are trapped. Don't move, it'll be painful.

// This function checks if the enemy is in your attack range.
function inAttackRange(enemy){
    var distance = hero.distanceTo(enemy);
    // Almost all swords have attack range of 3.
    if (distance <= 3){
        return True;
    }else{
        return False;
   }
   return distance;
}

// Attack ogres only when they're within reach.
while(true) {
    // Find the nearest enemy and store it in a variable.
    var enemy = hero.findNearestEnemy();
    // Call inAttackRange(enemy), with the enemy as the
    // and save the result in the variable canAttack.
    var inAttackRange = enemy;
    // If the result stored in canAttack is True, then attack!
    if (enemy){
        if (inAttackRange == True){  // it tells me here there is a problem but I don't understand how to //send the function data to this if desition
            hero.attack(enemy);
            }
        }
}

Thanks for your comments.

Edit: did you solve it?

1 Like

No, I cannot.

for me, I don’t understand how to send the data of the function to while loop code to make the choise.

Hello, people, I share the solution with Javascript. It’s was pretty easy only a few lines I mistake. This is the solution on Javascript

(Solution removed)