Cloudrip mountain library tactician(python only) - I am stuck

Hi there,

I would like to seek your help please.

Level task: No humans are allowed to die
Below are my codes, which runs fine, but there was always some soldier died
pls help me!!!
(sorry if messy.)

Hushbaum has been ambushed by ogres!

She is busy healing her soldiers, you should command them to fight!

The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!

Soldiers spread out in a circle and defend

def commandSoldier(soldier, soldierIndex, numSoldiers):
angle = Math.PI * 2 * soldierIndex / numSoldiers
defendPos = {“x”: 41, “y”: 40}
defendPos.x += 10 * Math.cos(angle)
defendPos.y += 10 * Math.sin(angle)
hero.command(soldier, “defend”, defendPos);

Find the strongest target (most health)

This function returns something! When you call the function, you will get some value back.

def findStrongestTarget():
bestTarget = None
enemies = hero.findEnemies()
# Figure out which enemy has the most health, and set bestTarget to be that enemy.
for i in range(len(enemies)):
enemy = enemies[i]
if mostHealth < enemy.health:
mostHealth = enemy
bestTarget = enemy
# Only focus archers’ fire if there is a big ogre.
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None

If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.

def commandArcher(archer):
nearest = archer.findNearestEnemy()
archerTarget = findStrongestTarget()
if archerTarget:
hero.command(archer, “attack”, archerTarget)
elif nearest:
hero.command(archer, “attack”, nearest)

archerTarget = None

while True:
# If archerTarget is defeated or doesn’t exist, find a new one.
# Set archerTarget to be the target that is returned by findStrongestTarget()
#friends = hero.findFriends()
soldiers = hero.findByType(“soldier”)

#if not archerTarget or archerTarget.health <= 0:
# Create a variable containing your archers.
archers = hero.findByType("archer")
for i in range(len(soldiers)):
    soldier = soldiers[i]
    commandSoldier(soldier, i, len(soldiers))
# use commandArcher() to command your archers
for i in range(len(archers)):
    archer = archers[i]
    commandArcher(archer)

Welcome‌ ‌welcome‌ ‌to‌ ‌the‌ ‌forum!‌ ‌It‌ ‌is‌ ‌a‌ ‌lovely‌ ‌place‌ ‌where‌ ‌you‌ ‌can‌ ‌share‌ ‌all‌ ‌kinds‌ ‌of‌ stuff‌ ‌(appropriate‌ ‌of‌ ‌course),‌ ‌share‌ ‌bugs,‌ ‌and‌ ‌even‌ ‌get‌ ‌assistance‌ ‌for‌ ‌code!‌ ‌We‌ ‌suggest‌ ‌that‌ ‌you‌ ‌review‌ ‌this‌‌ topic‌ ‌which‌ ‌shows‌ ‌all‌ ‌essentials‌ ‌of‌ ‌this‌ ‌board!‌ ‌And‌ ‌we‌ ‌suggest‌ ‌you‌ ‌review‌ this topic‌ ‌which‌ ‌shows‌ ‌how‌ ‌to‌ ‌post‌ ‌your‌ ‌code‌ ‌correctly!‌ ‌Thanks!!‌ ‌ :partying_face: :partying_face:
Please format your code correctly so we can see what is going on.
Lydia

Your code should look like this after it’s correctly formatted:

# Hushbaum has been ambushed by ogres!
# She is busy healing her soldiers, you should command them to fight!
# The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!
# Soldiers spread out in a circle and defend
def commandSoldier(soldier, soldierIndex, numSoldiers):
    angle = Math.PI * 2 * soldierIndex / numSoldiers
    defendPos = {“x”: 41, “y”: 40}
    defendPos.x += 10 * Math.cos(angle)
    defendPos.y += 10 * Math.sin(angle)
    hero.command(soldier, “defend”, defendPos);

# Find the strongest target (most health)
# This function returns something! When you call the function, you will get some value back.
def findStrongestTarget():
    bestTarget = None
    enemies = hero.findEnemies()
    # Figure out which enemy has the most health, and set bestTarget to be that enemy.
    for i in range(len(enemies)):
        enemy = enemies[i]
        if mostHealth < enemy.health:
            mostHealth = enemy
            bestTarget = enemy
    # Only focus archers’ fire if there is a big ogre.
    if bestTarget and bestTarget.health > 15:
        return bestTarget
    else:
        return None

# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
def commandArcher(archer):
    nearest = archer.findNearestEnemy()
    archerTarget = findStrongestTarget()
    if archerTarget:
        hero.command(archer, “attack”, archerTarget)
    elif nearest:
        hero.command(archer, “attack”, nearest)

archerTarget = None

while True:
    # If archerTarget is defeated or doesn’t exist, find a new one.
    # Set archerTarget to be the target that is returned by findStrongestTarget()
    #friends = hero.findFriends()
    soldiers = hero.findByType(“soldier”)
    #if not archerTarget or archerTarget.health <= 0:
    # Create a variable containing your archers.
    archers = hero.findByType("archer")
    for i in range(len(soldiers)):
        soldier = soldiers[i]
        commandSoldier(soldier, i, len(soldiers))
    # use commandArcher() to command your archers
    for i in range(len(archers)):
        archer = archers[i]
        commandArcher(archer)

THANKS for helping me

did you solve the level? if so then tick the solution box on the post which helped u the most