# Coding problem with Ogre Encampment

**URL:** <https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020>\
**Category:** Uncategorized\
**Created:** [March 9, 2015, 10:59pm UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020 "2015-03-09T22:59:51Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Code\_Designer](https://avatars.discourse-cdn.com/v4/letter/c/ecc23a/32.png) [@Code\_Designer](https://discourse.codecombat.com/u/Code_Designer)\
**Post date:** [March 9, 2015, 10:59pm UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/1 "2015-03-09T22:59:51Z")

</div>

I’m having trouble with Ogre Encampment with the attack part.  
My coding looks like this:

```python
# If there is an enemy, attack it.
# Otherwise, attack the chest!

loop:
   # If there is an "ogre". attack it:
    self.attack(self.findNearestEnemy())
    
     # Otherwise, attack the chest!
    self.attack("Chest")

```

I’m running in Python.

Any thoughts or suggestions?

---

<div class="post-metadata">

**Author:** ![J\_F\_B\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/j_f_b_m/32/2756_2.png) [@J\_F\_B\_M](https://discourse.codecombat.com/u/J_F_B_M)\
**Post date:** [March 9, 2015, 11:10pm UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/2 "2015-03-09T23:10:37Z")

</div>

Help us help you.  
You say you have trouble with the level. To get the best help possible, you should tell us what happens, and what you expect to happen with your code.

* * *

In this particular case your missing an `if enemy:`-check. Always check if there is an enemy before trying to attack it.

> **Try to solve it yourself before looking in here**
>
> You can check for an enemy like this:
> 
> ```python
> loop:
> enemy = self.findNearestEnemy() # Can return undefined
> if enemy: # Checks if enemy != undefined
> self.attack(enemy) # Causes problems when enemy == undefined
> else:
> self.attack("Chest")
> 
> ```

---

<div class="post-metadata">

**Author:** ![Feinty](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/feinty/32/1869_2.png) [@Feinty](https://discourse.codecombat.com/u/Feinty)\
**Post date:** [March 9, 2015, 11:10pm UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/3 "2015-03-09T23:10:55Z")

</div>

Welcome to the forums Code\_Designer I see you have read the FAQ and formatted your code properly as most new members fail to do(I did it to) the first thing your code is getting stuck on the first attack because the chest is considered an enemy so the second statement is unnecessary

---

<div class="post-metadata">

**Author:** ![Feinty](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/feinty/32/1869_2.png) [@Feinty](https://discourse.codecombat.com/u/Feinty)\
**Post date:** [March 9, 2015, 11:11pm UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/4 "2015-03-09T23:11:20Z")

</div>

Dang JFBM you beat me to it!

---

<div class="post-metadata">

**Author:** ![Code\_Designer](https://avatars.discourse-cdn.com/v4/letter/c/ecc23a/32.png) [@Code\_Designer](https://discourse.codecombat.com/u/Code_Designer)\
**Post date:** [March 9, 2015, 11:48pm UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/5 "2015-03-09T23:48:44Z")

</div>

Does it need to be if enemy, or if ogre?

---

<div class="post-metadata">

**Author:** ![Code\_Designer](https://avatars.discourse-cdn.com/v4/letter/c/ecc23a/32.png) [@Code\_Designer](https://discourse.codecombat.com/u/Code_Designer)\
**Post date:** [March 9, 2015, 11:56pm UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/6 "2015-03-09T23:56:29Z")

</div>

I get killed when the thrower ( I think) and a orc appear on the scene at the same time. Also, my kill the nearest enemy line isn’t working, even though I put the clause I have in front of it.

---

<div class="post-metadata">

**Author:** ![matt](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@matt](https://discourse.codecombat.com/u/matt)\
**Post date:** [March 10, 2015, 6:09pm UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/7 "2015-03-10T18:09:52Z")

</div>

Now that you have new code, you’ll need to post that up so we can help. Also a little more detail than the line isn’t working would be good - is it never running, or showing an error (i.e. a red cross next to the line)?

---

<div class="post-metadata">

**Author:** ![Code\_Designer](https://avatars.discourse-cdn.com/v4/letter/c/ecc23a/32.png) [@Code\_Designer](https://discourse.codecombat.com/u/Code_Designer)\
**Post date:** [April 9, 2015, 11:17am UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/8 "2015-04-09T11:17:46Z")

</div>

Sorry, I haven’t been on here in a while… It’s showing a red error next to the line that says, “Fix your Code: Target line is null, is there always a target to attack? Use if and else.”

```python
loop:
  # Use if/else.
  
  # If there are no enemies, attack the "chest"
  self.attack("Chest")
  
  # Else, attack the "enemy"
  self.attack(self.findNearestEnemy())

```

---

<div class="post-metadata">

**Author:** ![Code\_Designer](https://avatars.discourse-cdn.com/v4/letter/c/ecc23a/32.png) [@Code\_Designer](https://discourse.codecombat.com/u/Code_Designer)\
**Post date:** [April 9, 2015, 11:18am UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/9 "2015-04-09T11:18:39Z")

</div>

I also tried not doing the attack the chest, and that whole programming didn’t work.

---

<div class="post-metadata">

**Author:** ![Vlevo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vlevo/32/2074_2.png) [@Vlevo](https://discourse.codecombat.com/u/Vlevo)\
**Post date:** [April 10, 2015, 6:18am UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/10 "2015-04-10T06:18:07Z")

</div>

As J\_F\_B\_M said [above](http://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/2) - You have do what the comments say (iow: insert the code they tell you to)…

```python
# If there are no enemies, attack the "chest"

```

and

```python
# Else, attack the "enemy"

```

Otherwise you aren’t **following directions** and shouldn’t expect your code to work. 😄

---

<div class="post-metadata">

**Author:** ![Code\_Designer](https://avatars.discourse-cdn.com/v4/letter/c/ecc23a/32.png) [@Code\_Designer](https://discourse.codecombat.com/u/Code_Designer)\
**Post date:** [April 17, 2015, 10:09pm UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/11 "2015-04-17T22:09:46Z")

</div>

Got it! Thanks everyone! I realized after restarted it a few times that I had a problem with my indentation!

---

<div class="post-metadata">

**Author:** ![Charlotte\_Dunn](https://avatars.discourse-cdn.com/v4/letter/c/8dc957/32.png) [@Charlotte\_Dunn](https://discourse.codecombat.com/u/Charlotte_Dunn)\
**Post date:** [June 4, 2015, 8:08am UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/12 "2015-06-04T08:08:56Z")

</div>

I write the code below but i dont do any attacking at all. It says that I see an enemy but never attacks  
loop:  
enemy=self.findNearestEnemy()  
if enemy:  
self.attack(enemy)  
self.attack(enemy)

---

<div class="post-metadata">

**Author:** ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)\
**Post date:** [June 4, 2015, 5:16pm UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/13 "2015-06-04T17:16:27Z")

</div>

Hello, Charlotte. Please format your code properly according to the FAQ. Otherwise, it’s hard to tell what’s wrong with your code.

---

<div class="post-metadata">

**Author:** ![Vlevo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vlevo/32/2074_2.png) [@Vlevo](https://discourse.codecombat.com/u/Vlevo)\
**Post date:** [June 5, 2015, 3:19am UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/14 "2015-06-05T03:19:29Z")

</div>

Make sure you have indented everything that you expect to be in the "loop:"  
if you have say:

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

```

it will loop forever doing only “enemy=…” and never get to the “if” and so never attack…

---

<div class="post-metadata">

**Author:** ![nightminer1](https://avatars.discourse-cdn.com/v4/letter/n/c6cbf5/32.png) [@nightminer1](https://discourse.codecombat.com/u/nightminer1)\
**Post date:** [March 6, 2016, 11:26pm UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/15 "2016-03-06T23:26:16Z")

</div>

**same** DOES NOT WORK! My code:

enemy = self.findNearestEnemy()

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

---

<div class="post-metadata">

**Author:** ![Nicholas\_Freeland](https://avatars.discourse-cdn.com/v4/letter/n/6de8d8/32.png) [@Nicholas\_Freeland](https://discourse.codecombat.com/u/Nicholas_Freeland)\
**Post date:** [December 15, 2016, 2:51am UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/16 "2016-12-15T02:51:31Z")

</div>

I also am having trouble making mine work. This is what my code looks like. There is no error message that pops up, but my guy doesn’t do anything. He just stands there. How can I fix this?

if hero.findNearestEnemy():  
enemy = hero.findNearestEnemy()  
hero.attack(enemy)  
else:  
hero.attack(“Chest”)

---

<div class="post-metadata">

**Author:** ![Serg](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/serg/32/3260_2.png) [@Serg](https://discourse.codecombat.com/u/Serg)\
**Post date:** [December 15, 2016, 6:59pm UTC](https://discourse.codecombat.com/t/coding-problem-with-ogre-encampment/3020/17 "2016-12-15T18:59:38Z")

</div>

Are you using a while true loop?
