[SOLVED] I am stuck on operation: killdeer in sarven desert. (python)

This is my code:
enemy = hero.findNearestEnemy()

def shouldRun():
if hero.health < hero.maxHealth / 2:
return True
else:
return False

while True:

if shouldRun() == True:
    hero.moveXY(75, 37)
    


else:
    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! (Yay, I beat @enPointe77 to it :D)
Hi, please format your code correctly. :slight_smile:
You can look at how to do it here.

2 Likes

This time, I will format it for you.

enemy = hero.findNearestEnemy()
def shouldRun():
    if hero.health < hero.maxHealth / 2:
        return True
    else:
        return False
while True:
    if shouldRun() == True:
        hero.moveXY(75, 37)
    else:
        hero.attack(enemy)

This was your code, correct?

may be turn

else:
    hero.attack(enemy)

to

elif shouldRun==False:
    hero.attack(enemy)

also change

if shouldRun()==True:

to

if shouldRun==True:

I think?

No, shouldRun is a function, not a variable.

Same thing. (But you need to add brackets to shouldRun)

Remove

shouldRun() == True

and just replace it with

shouldRun()

because it does the same thing.
And if you want your code even shorter, then just delete the shouldRun function and just do

    if hero.health < hero.maxHealth / 2:
        hero.moveXY(75, 37)

in the while loop :grin:
also, the only problem that is not optimizing that I can see is that you only define enemy once, while you should be defining it constantly while the while-loop is running.

1 Like

thanks for the help! it wasnt working because i forgot to define enemy in the while true loop as you said. Thanks!

4 Likes