# [SOLVED] Library Tactician Help Please

One soldier crashes into one of the archers and there is a error that pops up saying, “try hero.findNearestEnemy”)

``````# 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():
mostHealth = 0
bestTarget = None
enemies = hero.findEnemies()
# Figure out which enemy has the most health, and set bestTarget to be that enemy.
for enemy in enemies:
if enemy.health > mostHealth:
bestTarget = enemy
mostHealth = enemy.health
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()
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.
if not archerTarget or archerTarget.health <= 0:
# Set archerTarget to be the target that is returned by findStrongestTarget()
archerTarget = findStrongestTarget()

friends = hero.findFriends()
soldiers = hero.findByType("soldier")
# 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
commandArcher()

``````
1 Like

Delete this

Should be inside

``````for archer in archers:

``````

and should be

``````commandArcher(archer)
``````

Lydia

1 Like

Where do I put for archer in archers:

2 Likes

Like this:

``````for archer in archers:
commandArcher(archer)
``````

at the very bottom of your while-true loop.
Lydia

1 Like

Sorry it’s taking so long it’s giving me the code never finished error.

2 Likes

It should work if you fixed what I said to fix.
Lydia

1 Like

I tried submitting it but then one of the soldiers died.

2 Likes

I passed the level with your code

Between here, I think you just forgot to indent.
Lydia

1 Like

Could you delete your code, since it is working?
Lydia

1 Like

I deleted it. (20 chars)

3 Likes

It’s still giving me the “code never finished” error, i’m going to reset my raspberry pie.

2 Likes

Check if you indented where you are supposed to, because your code, there was somewhere that wasn’t indented, and that might be the case

Lydia

1 Like

I did i’m running it right now.

3 Likes

Now one of the soldiers died.

2 Likes

Can you send me a screenshot?
Lydia

1 Like

Might be my equipment.

2 Likes

I don’t know how. (20 chars)

2 Likes

If you’re on Windows, you can press Windows Key + Shift + S to take a screenshot, and drag it to the area you want to screenshot, and come to the Discourse tab, and just paste it. (Ctrl + V)
Lydia

1 Like

Do you have Boss Star II or higher equipped? And a good glasses equipped?
Lydia

1 Like

I’m on linux. and yes I have boss star II

2 Likes