# Please Help. Blackwoods Brawl (python)

**URL:** <https://discourse.codecombat.com/t/please-help-blackwoods-brawl-python/2803>\
**Category:** Uncategorized\
**Created:** [February 20, 2015, 8:07pm UTC](https://discourse.codecombat.com/t/please-help-blackwoods-brawl-python/2803 "2015-02-20T20:07:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrShnap1](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@MrShnap1](https://discourse.codecombat.com/u/MrShnap1)\
**Post date:** [February 20, 2015, 8:07pm UTC](https://discourse.codecombat.com/t/please-help-blackwoods-brawl-python/2803/1 "2015-02-20T20:07:37Z")

</div>

Tell me please where an error in the code:

```
 loop:
    enemy = self.findNearestEnemy()
    distance = self.distanceTo(enemy)
    if self.isReady("cleave"):
        if distance < 10:
            self.cleave(enemy)
    else: 
        self.attack(enemy)

```

And why this error: distanceTo target is null. Does the target exist?(Use if?)  
If this code is correct:

```
loop:
    enemy = self.findNearestEnemy()
    distance = self.distanceTo(enemy)
    if distance < 10:
        # Attack if they get too close to the peasant.
        self.attack(enemy)
    else:
        self.moveXY(40, 37)

```

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![sotonin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sotonin/32/24089_2.png) [@sotonin](https://discourse.codecombat.com/u/sotonin)\
**Post date:** [February 20, 2015, 8:08pm UTC](https://discourse.codecombat.com/t/please-help-blackwoods-brawl-python/2803/2 "2015-02-20T20:08:22Z")

</div>

place three ` characters (on keyboard next to 1) on a line of it’s own both before and after your pasted code for formatting so people can read it here.

---

<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:** [February 20, 2015, 9:00pm UTC](https://discourse.codecombat.com/t/please-help-blackwoods-brawl-python/2803/3 "2015-02-20T21:00:12Z")

</div>

yes “enemy = findNearest()” can be null, even when it makes absolutely no sense for it to be null…

if enemy:  
&nbsp;&nbsp;&nbsp;&nbsp;distance = …

or in your current case it would prolly make sense to combine the ifs (since you only use distance for the if).

if enemy:  
&nbsp;&nbsp;&nbsp;&nbsp;if self.isReady(“cleave”) and self.distanceTo(enemy) \< 10:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.cleave(enemy)  
&nbsp;&nbsp;&nbsp;&nbsp;else:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.attack(enemy)

---

<div class="post-metadata">

**Author:** ![bmmtstb](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bmmtstb/32/12265_2.png) [@bmmtstb](https://discourse.codecombat.com/u/bmmtstb)\
**Post date:** [February 21, 2015, 8:40pm UTC](https://discourse.codecombat.com/t/please-help-blackwoods-brawl-python/2803/4 "2015-02-21T20:40:42Z")

</div>

try to change it like this (like @Vlevo did). the code should only have a distance to enemy when an enemy is there:

```python
if enemy:
   distance = self.distanceTo(enemy) # because now its for sure that an enemy is defined, so there is a distance wich can be defined.

```

so if u check an enemy for things, always make sure it is defined that an enemy is there. best code always has “if enemy: run some code”

or u use the much shorter way with and:

```python
if enemy and self.distanceTo(enemy) <= x:
   #run some code

```
