# Why is it not working?

In the screenshot above I got an error that basically says that archer.findNearestEnemy is undefined. But I am confused because archers are defined, what did I do wrong?

findNearestEnemy is only possible to use by hero/self. Even though you defined archer, it doesnâ€™t work.

no thats not the case:

You can only use it like this

``````for archer in archers:
enemy = archer.findNearestEnemy()
if enemy:
hero.command(archer, "attack", enemy)
``````

it lets the archer use it in for-loops.
also while-loops in the mountain are inefficient and it wants you to learn for-loops.

2 Likes

oh btw what is this lvl? (I think I seen it a long time b4 but I forgot)

The level is Library Tactician

Ok, The error is fixed but my archers arenâ€™t targetting the right guys for some reason, why?

``````# 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()
enemy = enemies[i]
while enemies < len(enemies):
# Figure out which enemy has the most health, and set bestTarget to be that enemy.
if enemy:
if enemy.health > mostHealth:
bestTarget = enemy

mostHealth = enemy.health

enemies += 1
# 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):
archers = hero.findByType("archer")
archer = archers[i]
for archer in archers:
nearest = archer.findNearestEnemy()
archerTarget = findStrongestTarget()
if archerTarget:
hero.command(archer, "attack", archerTarget)
else:
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
for i in range(len(archers)):
archer = archers[i]
commandArcher(archer)

``````

I do not know 100% exactly what causes your issue, but I do strongly suggest:

1. as already advised to you by herokolbert above, donâ€™t use while-loops because in the mountain you have to learn to use for-loops. You are using a while-loop in the findStrongestTarget() function. As far as I can tell, you are executing something in this while-loop wrong, but I will not help you fix it: instead, try using a for-loop (which is also simpler in a sense, less things to go wrong) as suggested in previous and easier levels.
2. in the commandArcher() part, donâ€™t repeat code that is already inside the while-True loop below it. This function is meant only for defining behavior related to whatâ€™s in the # comment above it. In other words, what you write here is limited to the actual commands.
The rest of your code seems fine, so good work, but you really need to take the for-loop stuff seriously to be able to cope with the rest of this map.
2 Likes

I figured it out! I just did not defined mostHealth further But now I know!

2 Likes

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