Hit and Freeze, glitch or bug?[SOLVED]

Hi, I’m having difficulties with the level, “Hit and Freeze”.
No matter what I do, I always start off dead.
I believe this is a glitch in the problem, but check my coding and tell me what you think.

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

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!

Please format your code correctly. Press the < / > button, and paste your code in there.

Thanks,

I formatted it correctly now. But no matter what I do, even if I comment all the code, I die at the start.
I copy the code here again.

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

Delete the if enemy part. Next, replace canAttack == True with canAttack.

Hello, I’ve followed your directions. This is what I have now, but it has made no difference.
I still die from the very beginning.

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

1 Like

What is your equipment? Can you provide a screenshot of your equipment? Your current code works for me fine.

My equipment? Sure.

1 Like

Can you show us a screenshot of when you die?

I think you need more armor

2 Likes

Yes, equip your armor.

That seems to be the issue. You need armor, or the bear trap will immediately kill you. You have the armor needed, so equip it to complete the level.

2 Likes

Then you don’t even have a chance of winning

1 Like

I have bought and equipted better armor. You were right, thank you. I have passed the level.

2 Likes

Great now mark the post that helped you solve you problem with a little checkmark, thanks.

Can you please mark the post it is right next to the :heartbeat: it looks like this :heavy_check_mark:

I don’t have that option . . . sorry.

1 Like

Oh sorry you are not a Basic User yet.

Yup that’s probably it.

1 Like

Just stick around read some topics then you should be able to do it

2 Likes

I will get back to you in the near future. Then I will do it.

2 Likes