Level Hit and Freeze is incredibly frustrating

What’s wrong with my code here?

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

canAttack = inAttackRange(enemy)

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

Like I’ve said, please post your code correctly as it says in this. And please post a link and your equipment for this level.
Lydia

@Michael_Joseph, happy to help you on your level, but could you please post your code like @Lydia_Song said? It looks like you did that with some of your code, but not all of it. If you don’t want to read through guidelines of posting your code, pretty much the summary is just clicking this button: image and pasting your code there. Please do this in the future so it is easier for us to read your code, but I think that I can still see your issue. It looks like you got a little confused by the instructions and mixed up a few things. The first issue is when you wrote:

What it wants you to do here is save the result of inAttackRange(enemy) to canAttack. It should look something like this:

canAttack = inAttackRange(enemy)

The other issue with your code is this:

The issue in your code here is that you needed to replace inAttackRange with canAttack. You just need to change that to look like this:

if canAttack == True:

And then you’re code should work. Hope this helps!
Grzmot

2 Likes