Leave it to Cleaver help? [SOLVED]

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

This is the code I’m using. It leaves 2 enemies left and the level is failed. I honestly don’t know what I’ve done wrong lol

In your function, you unnecessarily define a variable “ready” and then don’t even call it anywhere. This line should be removed and replaced with an if conditional, if hero.isReady("cleave"): and then tab everything over accordingly.

I didn’t even notice that.
This is my new code

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)

it still leaves a couple of enemies left

…I’ve been stuck on this level for 2 days…

and all i had to do was move the else hero.attack(target)over.

2 Likes

Hi I am having trouble with this level and I need someone to plz reply to me as soon as possible here is my code plz tell me how to improve it because my hero dies after 21 seconds

# 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`!
        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)

You’ve created a variable here:

But you haven’t used it.

Also here:

You haven’t done what it says. (“else”)
I hope this helps you.
:lion: :lion: :lion:

1 Like

I put else in but it automatically puts “If enemy”

So i put else is says error see

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(enemy)

This line:

 ready = hero.isReady("cleave")

should be an if statement, not a variable definition. Once you do that, don’t forget to tab over the statement immediately below it.

1 Like

can you show me what it would look like because it gave me an error

I have answered your question with a picture in the previous topic. ( You have asked the same question in four places :slight_smile: ) But again:
You can use:

if hero.isReady("cleave"):
    #  your code here
# or 
ready = hero.isReady("cleave")
if ready:
    #  your code here
2 Likes

thx i forgot about that

also the if hero.isReady("cleave"): doesnt work it gave me an error
sorry

THANK YOU SO MUCH xython

That if statement is correct. What error are you getting? Please post your code again and we’ll keep helping you until you get it right.

Continuing the discussion from Leave it to Cleaver help? [SOLVED]:

Hi everyone. im having trouble AND I NEED THE WHOLE CODE! Plz reply it below
Thanks

idk why but it just leaves me with one ogre left. i wish i could use my claymore sword but it is restricted. i need help DESPERATELY! It just doesnt give me enough time! :sob:

We don’t post the solutions on this forum. Show us your code and then we will try to help you find out what is wrong with your code.

never mind. i got it.