Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Level: Leave it to Cleaver

Until you learn to properly post your code here - from the game - NOT by copying and pasting it from this board, help is going to be limited because we can’t see the structure of your code. Did you even look at the link I posted explaining how to do that? You must use the </> button. Please read and follow these directions when posting code. There are many people here who are willing and able to help, but you must apply a little effort to enable them to do so.

1 Like

I finished the level sorry for getting back late

ive been stuck on this level for weeks and nothing has come so i need help

You’ve come to the right place for help, but unless you post your code we can’t see what’s wrong.

Please tell me how this does not work.

def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        hero.cleave(target)
    else:
        hero.attack(target)
while True:
    target = hero.findNearestEnemy()
    if enemy:
        cleaveWhenClose(target)

You’ve not defined enemy. Try if target:

Nope the last 2 still live.

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)
1 Like

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

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.