[SOLVED] Why do you need 'if enemy:'?

Hello, welcome to the CodeCombat forum. This post will help with formatting the code correctly in the forum.

To answer your question, when you give a command the code needs to complete the command before it will continue. Since your enemy variable didn’t find anyone, the hero doesn’t have anyone to attack. But the hero can’t do anything else until it attacks the non-existent enemy which stops your code from continuing.

The if enemy checks to see if there is a value in the variable enemy. If there is an enemy it returns True, now attack. If no enemy False skip the attack and check for another enemy. This ensures there is an enemy to attack before calling the command to attack so your code doesn’t lock up.

1 Like