Leave it to the cleaver (Python) [Solved]

So this is my code and works for cleaving but when it needs to use attack it won’t and it ends up dying

# 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(target)
    
        # 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)

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!

Welcome to the discourse! :partying_face:

Could you send a screenshot of your equipment?

Rachel

Could I have a link to the level? Thanks :slight_smile:

Rachel

CodeCombat - Coding games to learn Python and JavaScript? @Rachel699.

2 Likes

You haven’t defined enemy yet :slight_smile:

Rachel

1 Like

I can’t believe I missed that, thanks.

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.