# Backwoods Forest Code Python

**URL:** <https://discourse.codecombat.com/t/backwoods-forest-code-python/10251>\
**Category:** Uncategorized\
**Created:** [January 10, 2017, 9:42pm UTC](https://discourse.codecombat.com/t/backwoods-forest-code-python/10251 "2017-01-10T21:42:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![NOVA\_Renaissance\_Pro](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nova_renaissance_pro/32/5573_2.png) [@NOVA\_Renaissance\_Pro](https://discourse.codecombat.com/u/NOVA_Renaissance_Pro)\
**Post date:** [January 10, 2017, 9:42pm UTC](https://discourse.codecombat.com/t/backwoods-forest-code-python/10251/1 "2017-01-10T21:42:59Z")

</div>

I’m having problems with my code… I’ve tried to fix it but to no a vale. This is it:

loop:  
self.moveXY(43,35)  
self.moveXY(58,52)  
enemy = self.findNearestEnemy()  
if enemy:  
self.attack(enemy)  
else:  
self.moveXY(43,35)  
self.moveXY(58,16)  
self.findNearestEnemy()  
if enemy:  
self.attack(enemy)  
else:  
self.moveXY(40,31)

He doesn’t attack the second munchkin in the bottom self.findNearestEnemy()  
if enemy:  
self.attack(enemy)  
I can’t figure it out but I’m a beginner.

---

<div class="post-metadata">

**Author:** ![Ty\_Ler](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ty_ler/32/5322_2.png) [@Ty\_Ler](https://discourse.codecombat.com/u/Ty_Ler)\
**Post date:** [January 10, 2017, 10:53pm UTC](https://discourse.codecombat.com/t/backwoods-forest-code-python/10251/2 "2017-01-10T22:53:16Z")

</div>

Its hard to read your code. Can you please format it correctly by adding 3 backtick before and after the code or press the button that looks like `</>` at the top of the text editor.

The reason its not attacking is because all you called was `self.findNearestEnemy()` You need to make it look like the line on the top where it says `enemy = self.findNearestEnemy()`

---

<div class="post-metadata">

**Author:** ![NOVA\_Renaissance\_Pro](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nova_renaissance_pro/32/5573_2.png) [@NOVA\_Renaissance\_Pro](https://discourse.codecombat.com/u/NOVA_Renaissance_Pro)\
**Post date:** [January 11, 2017, 7:27pm UTC](https://discourse.codecombat.com/t/backwoods-forest-code-python/10251/3 "2017-01-11T19:27:01Z")

</div>

Ah. I’m sorry its my first time. Thanks I’ll try it and see if it works.

---

<div class="post-metadata">

**Author:** ![NOVA\_Renaissance\_Pro](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nova_renaissance_pro/32/5573_2.png) [@NOVA\_Renaissance\_Pro](https://discourse.codecombat.com/u/NOVA_Renaissance_Pro)\
**Post date:** [January 20, 2017, 1:23am UTC](https://discourse.codecombat.com/t/backwoods-forest-code-python/10251/4 "2017-01-20T01:23:00Z")

</div>

```python
loop:
    self.moveXY(43,35)
    self.moveXY(58,52)
    enemy = self.findNearestEnemy()
    if enemy:
        self.attack(enemy)
    else:
        self.moveXY(43,35)
        self.moveXY(58,16)
        enemy = self.findNearestEnemy()
        self.findNearestEnemy()
        if enemy:
            self.attack(enemy)
        else:
            self.moveXY(40,31)

```

Problem is that he goes to the top X and kills the munchkin and then he goes to the first X and back to the top one. By the time he gets to the bottom one the munchkin kills the other guy and then my guy kills him.

---

<div class="post-metadata">

**Author:** ![RenziYT](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/renziyt/32/5374_2.png) [@RenziYT](https://discourse.codecombat.com/u/RenziYT)\
**Post date:** [January 20, 2017, 3:06am UTC](https://discourse.codecombat.com/t/backwoods-forest-code-python/10251/5 "2017-01-20T03:06:13Z")

</div>

Ty\_ler can’t help you rn cause he’s in college,but he’ll reply to you when he would have time.

---

<div class="post-metadata">

**Author:** ![NOVA\_Renaissance\_Pro](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nova_renaissance_pro/32/5573_2.png) [@NOVA\_Renaissance\_Pro](https://discourse.codecombat.com/u/NOVA_Renaissance_Pro)\
**Post date:** [January 20, 2017, 3:08am UTC](https://discourse.codecombat.com/t/backwoods-forest-code-python/10251/6 "2017-01-20T03:08:50Z")

</div>

Ah, Ok I tried the code but he went to the top X marker and then went back to the starting spot.

---

<div class="post-metadata">

**Author:** ![BobBobson412](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bobbobson412/32/6871_2.png) [@BobBobson412](https://discourse.codecombat.com/u/BobBobson412)\
**Post date:** [January 20, 2017, 1:58pm UTC](https://discourse.codecombat.com/t/backwoods-forest-code-python/10251/7 "2017-01-20T13:58:07Z")

</div>

```python
loop:
    self.moveXY(43, 35)
    self.moveXY(58, 52)
    enemy = self.findNearestEnemy()
    if enemy:
        self.attack(enemy)
        self.moveXY(43, 35)
        self.moveXY(58, 16)
        enemy = self.findNearestEnemy()
        if enemy:
            self.attack(enemy)

```

If you don’t do the else it should just go to that next command and do both commands without any problems.

---

<div class="post-metadata">

**Author:** ![NOVA\_Renaissance\_Pro](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nova_renaissance_pro/32/5573_2.png) [@NOVA\_Renaissance\_Pro](https://discourse.codecombat.com/u/NOVA_Renaissance_Pro)\
**Post date:** [January 23, 2017, 8:26pm UTC](https://discourse.codecombat.com/t/backwoods-forest-code-python/10251/8 "2017-01-23T20:26:04Z")

</div>

Thanks it worked perfect. I didn’t know you could get rid of else statements and replace it with and if.

---

<div class="post-metadata">

**Author:** ![BobBobson412](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bobbobson412/32/6871_2.png) [@BobBobson412](https://discourse.codecombat.com/u/BobBobson412)\
**Post date:** [January 24, 2017, 1:56pm UTC](https://discourse.codecombat.com/t/backwoods-forest-code-python/10251/9 "2017-01-24T13:56:38Z")

</div>

Just don’t have an else statement after an if and it will still work and be fine.
