Leave it to Cleaver help? [SOLVED]

       # 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.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 my code. it isnt working again, leaving me with one munchkin left or 3 that kill me.
P.S this code is Python

u cannot use else: cause I tried it and they say we need to line up with if and if u use, ur hero does not move and im also stuck

SebastienDaMan u cannot use else I tried and it made my hero stuck rooted to da ground

it doesnā€™t work at all

You do realize that you are talking to someone who has not been active for about 2 years, right?

If you cannot use ā€˜elseā€™, it is most likely because you are incorrectly trying to implement itā€¦need some help with your code?

dont worry keep trying and itā€™ll work it happened to me that is what i did

Hi I am having trouble with this level and I need some help iā€™m like stuck on this for 6 days. Plz reply to me ASAP. When i do my code it leaves 2 enemies behind. I honestly donā€™t know anything to do. Plz help me.def cleaveWhenClose(target):
if hero.distanceTo(target) < 5:
if hero.isReady(ā€œcleaveā€):
hero.cleave(target)
else:
hero.attack(target)
while True:
target = hero.findNearestEnemy()
if target:
cleaveWhenClose(target)
this is my code plz help me to improve it.

def cleaveWhenClose(target):
if hero.distanceTo(target) < 5:
if hero.isReady(ā€œcleaveā€):
hero.cleave(target)
else:
hero.attack(target)
while True:
target = hero.findNearestEnemy()
if target:
cleaveWhenClose(target)

Hi 1styoutuber, welcome to discourse!

Weā€™re keen to help you, but please could you format your code and repost it. Thereā€™s instructions on how to do this here.

Thanks :slight_smile:.

1 Like
def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        if hero.isReady("cleave"):
            hero.cleave(target)
    else:
        hero.attack(target)
while True:
    target = hero.findNearestEnemy()
    if target:
        cleaveWhenClose(target)

thatā€™s my code Plz help to improve it

Put 4 spaces before these lines. Does it work now? I canā€™t remember the level.

1 Like

no its stills keeps leaving a couple behind

What is your code so far?

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

thats my code so far

I found I needed to put a hero.moveXY command in to the while True loop, so the hero moves back to the middle of the screen regularly. Otherwise she ends up fighting right at the top of the screen and misses some enemies with the cleave function.

wait you put hero.moveXY in the while true loop or in if target?

I put it in the while True loop, so right at the bottom of your code as itā€™s shown. Try it and see if it helpsā€¦

Thatā€™s just leave a bunch of ogres behind. This is my code so far.

        if hero.isReady("cleave"):
            hero.cleave(target)
    else:

MOD edit: Full code removed, as it is too close to a solution.

1 Like

plz help me improve plz plz

1 Like

Put before this four spaces, too