# "Patrol Buster" help?

**URL:** <https://discourse.codecombat.com/t/patrol-buster-help/2381>\
**Category:** Uncategorized\
**Created:** [January 18, 2015, 3:03am UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381 "2015-01-18T03:03:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![NashBama](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nashbama/32/1954_2.png) [@NashBama](https://discourse.codecombat.com/u/NashBama)\
**Post date:** [January 18, 2015, 3:03am UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/1 "2015-01-18T03:03:22Z")

</div>

Hi!

First, thanks so much for Code Combat. I’m working on changing careers by learning how to code. I’m fairly new to coding, so I’m sure there is a simple answer to this question that I’m just not seeing.

On “Patrol Buster”, I wrote a code where the warrior kills each enemy and returns to the center of the map. It works fine, but I keep getting an “Unexpected Token” error. It has to do with “dead”, but the code works. I tried re-writing several different ways using else, elif, and such, but I can’t figure it out. Below is my code, thanks for the help!

```python
loop:
    enemy = self.findNearestEnemy()
    if enemy:
    self.attack(enemy)
    if enemy dead:
    self.moveXY(35, 30)

```

I’ve also tried if enemy health = 0, same effect.

---

<div class="post-metadata">

**Author:** ![Roberto](https://avatars.discourse-cdn.com/v4/letter/r/8baadc/32.png) [@Roberto](https://discourse.codecombat.com/u/Roberto)\
**Post date:** [January 18, 2015, 4:12am UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/2 "2015-01-18T04:12:21Z")

</div>

Well, dead enemies aren’t returned by findNearestEnemy, so every “enemy” you see is alive.  
When there’s no nearest enemy is because there are no enemies alive on the map, so you can just add an `else:` instead of `if enemy.health == 0:`.

---

<div class="post-metadata">

**Author:** ![NashBama](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nashbama/32/1954_2.png) [@NashBama](https://discourse.codecombat.com/u/NashBama)\
**Post date:** [January 18, 2015, 10:26pm UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/3 "2015-01-18T22:26:36Z")

</div>

It still sees a dead enemy as a target, but the target is null. I get an error saying “Target is null, is there always a target to attack?”

I’ve tried different versions of else commands, but I can’t figure out exactly what it’s looking for to stop targeting a dead enemy and move to the next one.

---

<div class="post-metadata">

**Author:** ![jklauser](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jklauser/32/1867_2.png) [@jklauser](https://discourse.codecombat.com/u/jklauser)\
**Post date:** [January 18, 2015, 10:45pm UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/4 "2015-01-18T22:45:30Z")

</div>

You’re using Python which is sensitive to white-space, in order for the if statement to determine whether or not the attack method is called, it needs to be indented properly:

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

```

Otherwise you have:

```python
if enemy:
    # do nothing
self.attack(enemy) #always try to attack the enemy variable.

```

---

<div class="post-metadata">

**Author:** ![NashBama](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nashbama/32/1954_2.png) [@NashBama](https://discourse.codecombat.com/u/NashBama)\
**Post date:** [January 18, 2015, 11:30pm UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/5 "2015-01-18T23:30:11Z")

</div>

That fixed it! I didn’t realize indenting was that important. I fixed my indentations and everything ran perfectly.

Thanks very much for your help!

---

<div class="post-metadata">

**Author:** ![purplepuffball42](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/purplepuffball42/32/2182_2.png) [@purplepuffball42](https://discourse.codecombat.com/u/purplepuffball42)\
**Post date:** [March 16, 2015, 3:34pm UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/6 "2015-03-16T15:34:50Z")

</div>

> [@jklauser](#):
>
> else:  
> self.moveXY(…)

It doesnt work!!  
I need help, i’m stuck. already finished gems or death.

---

<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 16, 2015, 3:42pm UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/7 "2015-03-16T15:42:01Z")

</div>

Please include more info on your actual problem. What happens? Does it give you an error message? Does it behave in an unexpected way? Which one?

---

<div class="post-metadata">

**Author:** ![BrokenSyntax](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brokensyntax/32/34781_2.png) [@BrokenSyntax](https://discourse.codecombat.com/u/BrokenSyntax)\
**Post date:** [March 17, 2015, 1:41pm UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/9 "2015-03-17T13:41:51Z")

</div>

Yep, one of the reasons why I like Python, and suggest it to starting coders is because of the strict indentation.

Good code uses indentation to show levels and make it more readable, Python inherently enforces this practice.

---

<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 17, 2015, 2:29pm UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/10 "2015-03-17T14:29:23Z")

</div>

> [@BrokenSyntax](#):
>
> Python inherently enforces this practice

This is not true for example in JavaScript. The following is a totally valid JavaScript-Programm, but do you know what’s going on in there?

> **Bad Indentations**
>
> ```python
> loop{var items = this.findItems(); var best = null; var value = 0; for(var i = 0; i < items.length; i++){if (items[i].value > value){best = items[i]; value = best.value;}} this.move(best.pos);}
> 
> ```

Better is

> **Good Indentations**
>
> ```python
> loop{
> var items = this.findItems();
> var best = null;
> var value = 0;
> 
> for(var i = 0; i < items.length; i++){
> if (items[i].value > value){
> best = items[i]; 
> value = best.value;
> }
> } 
> this.move(best.pos);
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![purplepuffball42](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/purplepuffball42/32/2182_2.png) [@purplepuffball42](https://discourse.codecombat.com/u/purplepuffball42)\
**Post date:** [March 17, 2015, 3:08pm UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/11 "2015-03-17T15:08:01Z")

</div>

sorry it fixed itself there was a bug

---

<div class="post-metadata">

**Author:** ![danny\_li](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@danny\_li](https://discourse.codecombat.com/u/danny_li)\
**Post date:** [May 8, 2015, 2:37am UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/13 "2015-05-08T02:37:14Z")

</div>

its not working can someone help me  
[merged posts]

what code is that used for (asked of jklauser)

[merged posts]

here is part of the codei found out

enemy1 = self.findNearestEnemy()  
self.attack(enemy1)

---

<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:** [May 8, 2015, 2:54am UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/16 "2015-05-08T02:54:55Z")

</div>

“Hi, I need help” . . . doesn’t get anyone very far…  
What kind of help? What is wrong? What have you tried? what code did you use and what did it do or not do that you did or didn’t expect?  
(Read the FAQ before posting so you will have an idea of what to post and how to do so if you include code.)

---

<div class="post-metadata">

**Author:** ![wkc19](https://avatars.discourse-cdn.com/v4/letter/w/ed8c4c/32.png) [@wkc19](https://discourse.codecombat.com/u/wkc19)\
**Post date:** [July 8, 2015, 11:57am UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/17 "2015-07-08T11:57:53Z")

</div>

Im have the same problem but insted of self i put this  
help???

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [September 25, 2015, 7:24pm UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/18 "2015-09-25T19:24:26Z")

</div>

Hey this is Luke Patterson you, I started coding a month ago, and have gotten to backwoods forest, I don`t know if this is right but in the code, I don`t think it`s necessary to move to the enemy. I think you should let them come to you then kill them. Here`s the code that I did:  
loop:  
enemy = self.findNearestEnemy()  
# If there is an enemy, attack it!  
if enemy:  
self.attack(enemy)  
self.attack(enemy)

Hope this helps and good luck!!

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [September 25, 2015, 7:26pm UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/19 "2015-09-25T19:26:16Z")

</div>

Hey I did this code a few days ago. Could you send me the code you used?

---

<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:** [September 25, 2015, 11:11pm UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/20 "2015-09-25T23:11:49Z")

</div>

Please do not post working code. It gives an easy completion of a level without providing the intended learning experience.

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [September 25, 2015, 11:34pm UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/21 "2015-09-25T23:34:29Z")

</div>

I apologize from now on I will try to be more careful.

---

<div class="post-metadata">

**Author:** ![nukeulercake2004](https://avatars.discourse-cdn.com/v4/letter/n/df788c/32.png) [@nukeulercake2004](https://discourse.codecombat.com/u/nukeulercake2004)\
**Post date:** [October 29, 2015, 6:02pm UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/22 "2015-10-29T18:02:46Z")

</div>

it dont work why not??? 😕

---

<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:** [October 29, 2015, 10:59pm UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/23 "2015-10-29T22:59:49Z")

</div>

It’s been one month since this post has been active. Please leave the dead resting! Create your own topic if you need help.

---

<div class="post-metadata">

**Author:** ![PierceQuantum](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/piercequantum/32/5622_2.png) [@PierceQuantum](https://discourse.codecombat.com/u/PierceQuantum)\
**Post date:** [January 17, 2016, 3:03am UTC](https://discourse.codecombat.com/t/patrol-buster-help/2381/24 "2016-01-17T03:03:39Z")

</div>

This is what I used:  
[redacted, please don’t post correct solutions]

[Next page](https://discourse.codecombat.com/t/patrol-buster-help/2381.md?page=2)
