Need help using if statements


#1

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


#2

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


#3

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


#4

@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.