Need help using if statements

Im playing dueling grounds against cpu and my code is

hero.moveXY(63, 22)
enemy = hero.findNearestEnemy()
if enemy:
    distance = hero.distanceTo(enemy)
    if distance > 5:
        hero.moveXY(63, 22)
    else:
        hero.attack(enemy)

But even though the enemy is closer than 5 pixels he does not attack, pls help

Please include 3x backticks( ` ) before and after your code so it formats it properly so we can see the indentation.

I have done it now, it just stops after hero.move.

@Tinius the ``` are just for this forum so that you code prints nicely.

Are you looping your code or is this all of it? Loops allow the code to repeat each iteration of the game and allow the hero to try an attack again and again.

Without the loop, you are just asking the game: “Does an enemy exist, if not then do nothing.” only one time.

I need help to turn this statement into an “if” statement

ready    =    hero.isReady("cleave")

Do:

if hero.isReady("cleave"):
  hero.cleave(target)