Level: Leave it to Cleaver

The code is fine. Please respond with the hero you are using and post a screen shot of your equipment.

How do you take screenshots on a google Chromebook.

and Sir Tharin Thunderfist.

Screenshots with Chromebook…http://bfy.tw/JsVW

`

I should have noticed this before. In your function, you don’t check to see if hero is ready to cleave. The code is getting stuck because it is constantly trying to cleave, even during the cleave cooldown period, which is 10 seconds.

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

or it can be combined on a single line:

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

It work!!!

I searched for the full code here, but haven’t seen it, so here’s mine, that worked:

Mod edit: Please do not post final solutions.

when i use this code there are only 2 ogres left

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(enemy)
# else, just attack target!
else:
hero.attack(enemy)

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)
this is all my code including the hints with the #'s

Please learn to post your code correctly. The way it is now, we can’t see the structure. Help us help you. It’s very easy to do and just takes a tiny bit of effort. Please read this topic and format your code again correctly

1 Like

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(enemy)
else:
enemy = hero.findNearestEnemy()
hero.attack(enemy)

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)

not working for some reason

Hello,

Please create only one post for a problem. Adding to multiple topics won’t increase the help you get since everyone sees all of the new posts. I started working on your problem on the other topic.

Thank you dudes Sooo much for helping me solve the puzzle!! I was stuck on it for months, and it was kind of a bug for me.

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
hero.cleave(enemy)
# else, just attack target!
hero.attack(enemy)

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)

Hi @YaYeetingBOI, please read this topic to learn how to format your code properly:

Thanks
Danny

# 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
        hero.cleave(enemy)
        hero.attack(enemy)
        hero.attack(enemy)
        hero.moveXY(50, 12)
        hero.attack(enemy)
        # else, just attack `target`!
        hero.attack(enemy)
# 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`
        hero.attack(enemy)
        cleaveWhenClose(enemy)
findAndAttacknereastenmey


no matter how hard I try it won’t work. The only actions that happen are under If cleave is ready, then cleave target

nevermind i figured it out thanks though


I also use Sir Tharin Thunderfist, and it’s still not working!!!