How to cleave when multiple enemies nearby? (Python)

This problem can be spitted into two parts:

  • Finding the enemies that are close enough to you
  • Cleave if the amount of the close enough enemies is big enough to be worthwhile cleaving
def countNearbyEnemies(range_in_metres)
    count = 0
    enemies = hero.findEnemies():
    for enemy in enemies:
        if hero.distanceTo(enemy) < range_in_metres:
            count += 1
    return count
end

def worthwhileCleaving(minimumNearbyEnemiesCount):
    range = 5 # you can change it to whatever makes sense in a level
    count = countNearbyEnemies(range)
    return count >= minimumNearbyEnemiesCount

while True:
    if hero.isReady('cleave') and worthwhileCleaving(3): # more than 3 enemies 
        hero.cleave(hero.findNearest(hero.findEnemies()))
    else:
        # blah blah blah
        pass