Strange error (Noble sacrifice)

I get an error saying “friend” is undefined while it actually does get defined one line above the statement. I can pass the level just fine (with the lure yeti bonus) but my ally won’t attack the enemy ogres, probably due to this error, and I really want to understand what’s wrong. I originally had ‘‘soldier’’ instead of ‘‘friend’’ but I keep looking at a previous level to get this right, and nothing works.

while True:
    soldiers = hero.findFriends()
    for i in range(len(points)):
        point = points[i]
        friend = soldiers[i]
        enemy = friend.findNearestEnemy()
        if enemy and enemy.type != "yeti":
            hero.command(friend, "attack", enemy)
        else:
            hero.command(friend, "move", point)

if you want, after you define friend, make an if-statement checking if there is a friend in the first place, and then indent everything once under it

Thanks. Actually that works to filter out the error, and I would like to know why.
But at the same time, the aggressive stance actually makes the level fail because the yeti can’t see the last soldier.
So I guess it’s better without aggressive soldiers.
However, the point is still that I wonder why you need to check for a friend in the first place

Also, in ‘‘Zoo Keeper’’ it does not seem to be necessary to prevent an error