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

Level: Leave it to Cleaver

#12

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!
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 my code for leave it to the cleaver and it doesn’t work. The last two ogres aren’t getting attack. My family has tried it my brother and dad they both failed

#13

it’s very difficult to see the problem because your code isn’t properly formatted for this forum, so I can’t see the structure. The proper use of the triple backticks when posting code can be found here:

https://discourse.codecombat.com/faq

One glaringly obvious problem is that your function starts out referring to target in the arguments but then you switch and start referring to enemy in the arguments. Consistency is important. Enemy is not defined in the function, just the code. The code can’t read definitions in the function and the function can’t read definitions in the code.

1 Like
#14

I don’t understand what you said and I learned by looking at my code when the last two ogres come out it tries to cleave them and it doesn’t use the else to attack instead. Please send more help. Sorry for all the trouble

#15

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

I finished the level sorry for getting back late

#17

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

#18

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

#20

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

You’ve not defined enemy. Try if target:

#22

Nope the last 2 still live.

#23

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

#24

How do you take screenshots on a google Chromebook.

#25

and Sir Tharin Thunderfist.

#26

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

`

#27

#28

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

It work!!!

#30

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.

#31

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

#33

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