[SOLVED] Leave it to cleaver is not bugged

This code is correct according to your rules. And I can not pass that level as can not many others.
If you wish that I woud pay you money, you must do much better than that! Please remove the bug or correct my code. The first time when I ran this code it worked well, then I submitted and it failed like for many others

# This shows how to define a function called cleaveWhenClose
# The function defines a parameter called target
def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        pass
        # Put your attack code here
        # If cleave is ready, then cleave target
        ready = hero.isReady("cleave")
        hero.cleave(target)
        # else, just attack target!
    else:
        hero.attack(target)    

# This code is not part of the function.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Note that inside cleaveWhenClose, we refer to the enemy as target.
        cleaveWhenClose(enemy)

Two problems with your code:

  1. You changed the recommended indentation from the sample code, so your else is on the wrong line.
  2. You are not actually checking if you are ready to cleave. You call the function, but you don’t use an if-statement.

your advices are not helpful! and they are not clear. Indentation is correct as demanded by your site. What kinf of “IF” i need to put with regards to hero is ready part. By the way the cleave thing is working fine. It is the last Ogre that is left alive and that’s why the level is not finishing.
It is attack part that has to be defined as when ogres come too close the hero can not attack them. I can not rewrite the code because your website doesn’t permit to change it in a way that it would function.

check the bugs better.

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

nevermind I solved it

I’ve got a problem. I followed the rules Serg told Brigi using Brigi’s code, but it still didn’t work! I’ve been trying this level for so long. Can you show me the correct code?