Hunting Party (Python) - [Solved]

Hey all,
I keep getting error messages in this level. Could someone take a look please. I just get the error, no movement from my hero.

many thanks.

# You can use findNearestEnemy() on your soldiers to get their nearest enemy instead of yours.
while True:
    friend = hero.findFriends()
  # Use for-loop and for each friend:
    for i in range(len(friend)):
    #    # If they see an enemy then command to attack.
        friend = friend[i]
        enemy = friend.findEnemies()
        if enemy:
            hero.command(friend, "attack", enemy)
        # Command to move east by small steps.
    #x = friend.pos + 5
    #y = friend.pos
        
    hero.command(friend, "move", {"x": friend.pos.x +5, "y" : friend.pos.y })

1 Like

You are commanding your friend to attack an array. Instead of friend.findEnemies(), use friend.findNearestEnemy(). This way, they attack one target (the closest enemy).

1 Like

i tried that but unfortunately i get this.

# You can use findNearestEnemy() on your soldiers to get their nearest enemy instead of yours.
while True:
    friend = hero.findFriends()
  # Use for-loop and for each friend:
    for i in range(len(friend)):
    #    # If they see an enemy then command to attack.
        friend = friend[i]
        enemy = friend.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)
        # Command to move east by small steps.
    #x = friend.pos + 5
    #y = friend.pos
        
    hero.command(friend, "move", {"x": friend.pos.x +5, "y" : friend.pos.y })

1 Like

Change friend = hero.findFriends() to friends = hero.findFriends. Then change the for loop to:

for friend in friends:

Then delete friend = friend[i]. If there is an enemy, command your friends to attack it, otherwise, command them to move slowly to the right.

1 Like

@abc

thanks so much. these loops are annoying me. i need more practice.
btw, why doesn’t for i in range(len(friend)): work?

1 Like

I think it may be because you are using two variables that are the same, so make sure that when using hero.findFriends(), you define it as a different variable than the individual friends.

2 Likes

thank you for all your help!! :smiley:

1 Like

No problem. Glad to help. :slightly_smiling_face:

2 Likes

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