Long-range division(python)

I defeated the first enemy, but my hero doesn’t attack the second enemy…Anyone help please!

Howdy and welcome to the forum! Please post your code properly formatted. You can learn how, here: [Essentials] How To Post/Format Your Code Correctly

However, you are only defining the enemy one time…you define the first enemy on line 20, which you can then attack, but there is no code for defining another enemy.

should I define the second enemy after the while True loop? What should I do?

Define all enemies in the loop. Remember, it is a loop, so in the first iteration it would define enemy1, on the next iteration, it would define #2, and so on.

ok I see. But in some levels if the enemy = hero.findNearestEnemy is before the while true loop it still can attacks all enemies. Is it because these two are different type of enemy?

1 Like

Most likely, it was being used to “seed” the enemy variable. This way, ‘enemy’ has a value that can be used/manipulated inside the loop. Most likely, ‘enemy’ is later reseeded (refreshed) to update the variable to the current enemy state. In the case of this level, there are only 4 enemies, if you count the two rows of mines; no new enemies are being generated.

In these early levels, placing ‘enemy’ before the while loop is fairly safe…you have a set strategy and game-plan. In later levels, this will not necessarily be the case. Things can get a bit more involved and yes, you may have different goals for different enemy types…this will be covered in coming lessons.

btw, that was a very good question! I kind of erred in my explanation…at least, it is a little ambiguous.

In this level, you do need to define the enemy inside the loop, since there is more than one that needs to be attacked. The mines are handled by the artillery, so don’t actually count. With the definition inside, it will refresh with each iteration, until there are no more enemies, thereby making the while True statement = False…hence, ending the code.

thank you for replying! Your answers are so helpful!