Hit and Freeze Python

I am stuck on the level, “Hit and Freeze.”
This is 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!
hero.attack(enemy)
pass

PLEASE HELP:(:scream:

NEVER MIND I PASSED IT :slight_smile:

ACTUALLY NOT :frowning:
NO I DID
SERIOUSLY

@agarionotpro

I’m confused. Did you pass the level or not?

I finished the level

1 Like

which code did you use?

Mod edit: Please do not request solutions. The purpose of this board is to assist people with their code. Simply providing solutions is counter productive to the learning process. There are many people here willing and able to assist you with your code and help you figure out what the problem is, but we do not just give the answers. Thanks.

Please do not ask for codes please

1 Like

I can’t find to finish the level

Can you post your current code so we can help you?

1 Like

Wait, @SuperSmacker that’s a solution could you get rid of it. (actually it doesn’t really matter, do what you think)

here is the code

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!
hero.attack(enemy)
pass

this code attacks the munchkins and kills 5 exactly but when the others escape the hero follows it and takes too much damage from the trap.

Can you format your code so we can see clearly? Use the </> button to do that.

1 Like

Please learn to post your code properly. It’s really easy and only requires a very small amount of effort.

FormatCode

To post your code from the game, 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.

``` <— Triple back tick marks.

Paste ALL of your code in here.

``` <— Triple back tick marks.

There are many people here willing and able to help. If you use the </> button correctly, then ALL of 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 isn’t formatted 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.

But it takes us a long time to write these kind of posts, for just a tiny thing ;( LOL

1 Like

Nope. Copy and paste. I’ve posted that so many times that I have it saved.

1 Like

… (20 fainted @Chaboi_3000’s ) Maybe have a notepad of all copy and paste items. Then you can choose a variety of posts that are going to be repeated.

1 Like

Set up canned replies, it’s easier. Just click on the gear and set them up. :sunglasses:

I’m sad that only admins and mods have them, we only have the default options ;(

1 Like

Oh, didn’t know that the option wasn’t available to you. Someday…

Until then, use a word doc or .txt. Still fast and easy.

1 Like

Btw moderation in discord servers are tough when so many people hate each other, I’m like warn then kick then ban, and it’s making me crazy. You’re lucky you’re a mod of a peaceful chat because mines are indescribably out of control.

1 Like