# Leave it to the Cleaver

**URL:** <https://discourse.codecombat.com/t/leave-it-to-the-cleaver/9829>\
**Category:** Uncategorized\
**Created:** [December 5, 2016, 4:38am UTC](https://discourse.codecombat.com/t/leave-it-to-the-cleaver/9829 "2016-12-05T04:38:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![UmbroDominus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/umbrodominus/32/5285_2.png) [@UmbroDominus](https://discourse.codecombat.com/u/UmbroDominus)\
**Post date:** [December 5, 2016, 4:38am UTC](https://discourse.codecombat.com/t/leave-it-to-the-cleaver/9829/1 "2016-12-05T04:38:48Z")

</div>

My code works at the start but then it just falls apart!

def cleaveWhenClose(target):  
if hero.distanceTo(target) \< 5:  
pass  
ready = hero.isReady(“cleave”)  
hero.cleave(enemy)  
=  
else:  
hero.attack(enemy)  
while True:  
enemy = hero.findNearestEnemy()  
if enemy:  
# Note that inside cleaveWhenClose, we refer to the enemy as target.  
cleaveWhenClose(enemy)

---

<div class="post-metadata">

**Author:** ![FlowerChi](https://avatars.discourse-cdn.com/v4/letter/f/71c47a/32.png) [@FlowerChi](https://discourse.codecombat.com/u/FlowerChi)\
**Post date:** [December 5, 2016, 7:18am UTC](https://discourse.codecombat.com/t/leave-it-to-the-cleaver/9829/2 "2016-12-05T07:18:55Z")

</div>

the code is wrong and it doesn’t make sense let me try and do it correctly there are some error but I think you get the concept.

```python
def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        if hero.isReady("cleave"):
            hero.cleave(target)
    else:
        hero.attack(enemy)

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        cleaveWhenClose(enemy)

```

The mistakes I can see are  
Once in the function, you said enemy instead of target.  
And I can’t fully understand your if and else code for your function.

---

<div class="post-metadata">

**Author:** ![2287](https://avatars.discourse-cdn.com/v4/letter/2/9fc348/32.png) [@2287](https://discourse.codecombat.com/u/2287)\
**Post date:** [December 5, 2016, 8:06pm UTC](https://discourse.codecombat.com/t/leave-it-to-the-cleaver/9829/3 "2016-12-05T20:06:27Z")

</div>

def cleaveWhenClose(target):  
  if hero.distanceTo(target) \< 5 and hero.isReady(“cleave”):  
   hero.cleave(target)  
  else:  
    hero.attack(target)

while True:  
  enemy = hero.findNearestEnemy()  
  if enemy:  
    cleaveWhenClose(enemy)

---

<div class="post-metadata">

**Author:** ![xExaviorx](https://avatars.discourse-cdn.com/v4/letter/x/45deac/32.png) [@xExaviorx](https://discourse.codecombat.com/u/xExaviorx)\
**Post date:** [December 7, 2016, 2:25am UTC](https://discourse.codecombat.com/t/leave-it-to-the-cleaver/9829/4 "2016-12-07T02:25:17Z")

</div>

Part of your problem here, because i’m actually going to help and not just give the answer. Is that you are defining a variable for `hero.isReady(cleave);` really, there is no need to do this. But even if you do. you need a IF statement to say that only cleave if it is ready, otherwise it just gets stuck here! Also, the pass is unnecessary. And, for your function, you call target as the distance to, and for the def, but you never define target, you use the variable enemy instead.

---

<div class="post-metadata">

**Author:** ![UmbroDominus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/umbrodominus/32/5285_2.png) [@UmbroDominus](https://discourse.codecombat.com/u/UmbroDominus)\
**Post date:** [December 7, 2016, 3:10am UTC](https://discourse.codecombat.com/t/leave-it-to-the-cleaver/9829/5 "2016-12-07T03:10:33Z")

</div>

Thank you all for helping me!!

---

<div class="post-metadata">

**Author:** ![xExaviorx](https://avatars.discourse-cdn.com/v4/letter/x/45deac/32.png) [@xExaviorx](https://discourse.codecombat.com/u/xExaviorx)\
**Post date:** [December 7, 2016, 3:21pm UTC](https://discourse.codecombat.com/t/leave-it-to-the-cleaver/9829/6 "2016-12-07T15:21:21Z")

</div>

No problem sir. Just next time, make sure to format your code! You do that by highlighting your code and pressing the "\< / \> " button on the menu in replies.
