Cant solve this level pls help

Hello,

This is my first time posting and I am stuck on a level. This is my code: I don’t know what the problem is can someone please help me.

friends = self.findFriends()
for i in range(len(friends)):
friend = friends[i]
enemy = friend.findNearest(friend.findEnemies())
if enemy:
if friend.health > friend.maxHealth/1.5:
self.command(friend, “attack”, enemy)
else:
self.command(friend, “move”, {‘x’:friend.pos.x - 40, ‘y’:friend.pos.y})
else:
self.command(friend, “move”, {‘x’:friend.pos.x + 40, ‘y’:friend.pos.y})

Hello and welcome to the discourse. Can you please tell us which level is and please format your code correctly using the </> button. Thanks. Here are a couple of examples.

hero.say("Hello World")

or

while True:
   enemy = hero.findNearestEnemy()
   hero.attack(enemy)
   distance = distanceTo(enemy)
   if distance < 5:
     hero.cleave
   else:
      hero.attack(enemy)

Note: this is just a sample code

Do you still need help? If so, please let me know

Also, It would help to know what level you are experiencing difficulty with.

The level is called Hunting Party, hopefully I have formatted the code correctly, it says to use the code toolbar button but am not sure what that is.

‘’
friends = self.findFriends()
for i in range(len(friends)):
friend = friends[i]
enemy = friend.findNearest(friend.findEnemies())
if enemy:
if friend.health > friend.maxHealth/1.5:
self.command(friend, “attack”, enemy)
else:
self.command(friend, “move”, {‘x’:friend.pos.x - 40, ‘y’:friend.pos.y})
else:
self.command(friend, “move”, {‘x’:friend.pos.x + 40, ‘y’:friend.pos.y})
‘’

:] you can also surround your code with three back ticks like this one `

as for your code

  1. you have to put everything in a while-true loop (so that it doesnt only iterate once over your friends, rather every time the for-loop is done)
  2. change the 40 to 10 when you are adding a number to the “x” in the commands to move

probably that works

1 Like

Thank you for helping me I appreciate it.

1 Like