Leave it to Cleaver help? [SOLVED]


#1
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


#2

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.


#3

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


#4

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

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


#5

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)

#6

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:


#7

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


#9

So i put else is says error see


#10
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)

#11

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.


#12

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


#13

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

#14

thx i forgot about that


#15

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


#16

THANK YOU SO MUCH xython


#17

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.