Hunting Party - stuck! (Python)

Okay so here is my code

friends = hero.findFriends()

for friend in friends:
    enemy = friend.findNearest(friend.findEnemies())
    if not enemy:
        hero.command(friend, "move", {'x': friend.pos.x + 24, 'y': friend.pos.y})
    else:
        hero.command(friend, "attack", enemy)

I know the for loop is going to loop over each friend. I also tried it this way:

friends = hero.findFriends()

for friend in friends:
    enemy = friend.findNearest(friend.findEnemies())
    if enemy:
        hero.command(friend, "attack", enemy)
    else:
        hero.command(friend, "move", {'x': friend.pos.x + 24, 'y': friend.pos.y})

In each case all of my units move 24 units to the right and then the top units stand there and get attacked. I’m not sure what to do and unfortunately I’m really lost. I can’t figure out exactly what it’s asking me and the hints aren’t helping. Please advise.

From the hints:
HuntParty01

24 is WAY too much. Try putting a decimal point in front of that value. Then keep adjusting the number until it works.

BTW, your second code post should work, IF you put the whole thing (all of it) in a loop.

1 Like

All that happens when I change it is they move forward less and then stop.

it stops because it’s not looping through the code. There is no loop. Try while True for the whole thing.

That’s what I just did. I saw you adjust your earlier comment. That seems to be working but it’s being really laggy. Well, some troops died, but the while True loop fixed it. Thank you so much. I feel like I’m losing my mind today and this level was just the icing on the cake.

1 Like

now adjust that numeric value until it works and no one dies so you get the bonus.

1 Like

What do you want? Mk see ya.

I’m having trouble with this level. My troops move right, but when they see an enemy they don’t attack and actually move left. Here is my code:

while True:
    friends = hero.findFriends()
    enemy = hero.findNearestEnemy()
    # Use for-loop and for each friend:
    for friend in friends:
        # If they see an enemy then command to attack.
        if enemy:
            hero.command(friend, "attack", enemy)
        else:
            hero.command(friend, "move", {'x': friend.pos.x + 0.15, 'y': friend.pos.y})
        # Command to move east by small steps.

I don’t see nothing wrong with your code. Increase the value to 0.3 or 0.45 and submit several times.

I tried changing the number, but the troops still don’t attack.

OMG :slight_smile: Didn’t see that!
You have:

while True:
    friends = hero.findFriends()
    enemy = hero.findNearestEnemy()
   hero.say(enemy.id)
    # and so on... 


Do you see the one on the top right corner :slight_smile:
put:

    for friend in friends:
        # your friend must find the enemy

What? I don’t really understand.

All your warriors are attacking the enemy that’s closer to the hero - Pinakin. Every warrior must attack the enemy next to him. The first poster - Ian_Lambert-BrownBrown has done it right, ( I have put hero.say(enemy.id) for debug

What item allows “friend.findNearest(friend.findEnemies())” because I don’t have that, or is it already allowed?

This is same as friend.findNearestEnemy()

It says it’s an error.

It says it’s an error because you put the definition of enemy = friend.findNearestEnemy() before you defined friend in the for loop. Put the enemy definition inside the for loop.

Thanks for the advice! It works fine now.

What is the enemy definition

I need some help, here is my code

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