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

**URL:** https://discourse.codecombat.com/t/solved-why-do-you-need-if-enemy/19771
**Category:** Adventurer
**Created:** [December 7, 2019, 7:54pm UTC](https://discourse.codecombat.com/t/solved-why-do-you-need-if-enemy/19771 "2019-12-07T19:54:16Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)
#### Post date: [December 7, 2019, 8:39pm UTC](https://discourse.codecombat.com/t/solved-why-do-you-need-if-enemy/19771/2 "2019-12-07T20:39:11Z")

</div>

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

> [@\[Essentials\] How To Post/Format Your Code Correctly](https://discourse.codecombat.com/t/essentials-how-to-post-format-your-code-correctly/15521):
>
> Formatting your code before posting code is essential to our community, and if you don’t we might not be able to help frowning Everyone must know how to format ALL of your code properly. It’s really easy and only requires a very small amount of effort. To post your code from the game , use the \</\> button or it won’t format properly. When you click the \</\> button, the following will appear: Please paste ALL …

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.

---

_[View the full topic](https://discourse.codecombat.com/t/solved-why-do-you-need-if-enemy/19771)._
