Leave it to the Cleaver

My code works at the start but then it just falls apart!

def cleaveWhenClose(target):
if hero.distanceTo(target) < 5:
pass
ready = hero.isReady(“cleave”)
hero.cleave(enemy)
=
else:
hero.attack(enemy)
while True:
enemy = hero.findNearestEnemy()
if enemy:
# Note that inside cleaveWhenClose, we refer to the enemy as target.
cleaveWhenClose(enemy)

the code is wrong and it doesn’t make sense let me try and do it correctly there are some error but I think you get the concept.

def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        if hero.isReady("cleave"):
            hero.cleave(target)
    else:
        hero.attack(enemy)

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        cleaveWhenClose(enemy)

The mistakes I can see are
Once in the function, you said enemy instead of target.
And I can’t fully understand your if and else code for your function.

def cleaveWhenClose(target):
  if hero.distanceTo(target) < 5 and hero.isReady(“cleave”):
   hero.cleave(target)
  else:
    hero.attack(target)

while True:
  enemy = hero.findNearestEnemy()
  if enemy:
    cleaveWhenClose(enemy)

Part of your problem here, because i’m actually going to help and not just give the answer. Is that you are defining a variable for hero.isReady(cleave); really, there is no need to do this. But even if you do. you need a IF statement to say that only cleave if it is ready, otherwise it just gets stuck here! Also, the pass is unnecessary. And, for your function, you call target as the distance to, and for the def, but you never define target, you use the variable enemy instead.

1 Like

Thank you all for helping me!!

No problem sir. Just next time, make sure to format your code! You do that by highlighting your code and pressing the "< / > " button on the menu in replies.