Lvl. 5 ch.2 SOLVED

Hello,
I wrote this code but system doesn’t accept it, says that it’s an error in the if statement

loop:
enemy = self.findNearestEnemy()
if enemy:
self.attack(enemy)

P.S.
It worked when I retried today. Probably it was just a bug. -.-

Hello, Rijechanin, and welcome. Please format your code according to the FAQ.

You code looks sound. What is the error you get?

Hello
What you want to express (“if there’s any enemy”) is equivalent to what you coded (“if there’s a nearest enemy”). So basicaly, and as ChronistGilver said, your code is ok.

If you want to specifically use the “there is any enemy” statement, you can code it this way :

(self.findEnemies().length > 0)

or even

(self.findEnemies().length)

which means “the number of enemies is greater than zero” i.e. : “there is at least one enemy”

For example :

loop:
  enemy=self.findNearestEnemy()
  if self.findEnemies().length :
    self.attack(enemy)

But personally, i like your original code better…

Thanks, I changed it.

Pixx,
Thank you for explanation. I passed the level with my code.
Maybe the bug was created because I ereased comments…