# SARVEN DESERT: Help with Sarven Brawl using the Python Coding Language

**URL:** <https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222>\
**Category:** Level Help\
**Created:** [March 25, 2015, 2:10pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222 "2015-03-25T14:10:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![PikaNAW](https://avatars.discourse-cdn.com/v4/letter/p/f4b2a3/32.png) [@PikaNAW](https://discourse.codecombat.com/u/PikaNAW)\
**Post date:** [March 25, 2015, 2:10pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/1 "2015-03-25T14:10:32Z")

</div>

It is saying ‘enemy’ is null

```python
loop:
    enemy = self.findNearestEnemy()
    enemyindex = 0
    while enemyindex < 100:
        if enemy.type != "sand-yak":
            while enemy.health > 0:
                if self.isReady("bash"):
                    self.bash(enemy)
                else:
                    self.attack(enemy)
        elif self.findNearestEnemy():
            self.moveXY(93, 70)

```

---

<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:** [March 25, 2015, 3:10pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/2 "2015-03-25T15:10:29Z")

</div>

check for “enemy is ‘null’” in this forum, but to make it short:

```python
if enemy:
   do sth
else:
  do sth else

```

means that you first have to check if there is an enemy. That means when computer runs your cude and asks for

```
if enemy.type != "sand-yak":

```

it isnt for sure that there is an enemy, and he cant find the type of an enemy who is not there. So first chek for enemys

---

<div class="post-metadata">

**Author:** ![PikaNAW](https://avatars.discourse-cdn.com/v4/letter/p/f4b2a3/32.png) [@PikaNAW](https://discourse.codecombat.com/u/PikaNAW)\
**Post date:** [March 25, 2015, 4:41pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/3 "2015-03-25T16:41:57Z")

</div>

still saying the same thing

---

<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:** [March 25, 2015, 6:19pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/4 "2015-03-25T18:19:47Z")

</div>

whats your code now?

---

<div class="post-metadata">

**Author:** ![PikaNAW](https://avatars.discourse-cdn.com/v4/letter/p/f4b2a3/32.png) [@PikaNAW](https://discourse.codecombat.com/u/PikaNAW)\
**Post date:** [March 25, 2015, 6:32pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/5 "2015-03-25T18:32:35Z")

</div>

```python
loop:
    enemyindex = 0
    while enemyindex < 100:
        enemy = self.findNearest(self.findEnemies())
        if enemy.type != "sand-yak" and enemy.health > 0:
                if self.isReady("bash"):
                    self.bash(enemy)
                else:
                    self.attack(enemy)
        elif self.findNearest(self.findEnemies()):
            self.moveXY(93, 70)

```

---

<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:** [March 25, 2015, 6:46pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/6 "2015-03-25T18:46:46Z")

</div>

How can you say “still” when you didn’t make the change he suggested.

---

<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:** [March 25, 2015, 6:51pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/7 "2015-03-25T18:51:10Z")

</div>

you do not ask if there generally is an enemy. but i dont want to give you the right answer, because i want to find the solution on your own, because this is really importent!

---

<div class="post-metadata">

**Author:** ![PikaNAW](https://avatars.discourse-cdn.com/v4/letter/p/f4b2a3/32.png) [@PikaNAW](https://discourse.codecombat.com/u/PikaNAW)\
**Post date:** [March 26, 2015, 12:54am UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/8 "2015-03-26T00:54:22Z")

</div>

I did this code, and it WORKED!!

```python
loop:
    enemy = self.findNearest(self.findEnemies())
    if enemy and enemy.type != "sand-yak":
        self.attack(enemy)
    else:
        self.moveXY(91, 71)

```

---

<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:** [March 26, 2015, 8:22am UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/9 "2015-03-26T08:22:29Z")

</div>

jeah, thats the way you go. now if you want to, you can reuse your code from above and bash for example instead of just attacking 😉 but i think in this level its overrated

---

<div class="post-metadata">

**Author:** ![arduino731](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@arduino731](https://discourse.codecombat.com/u/arduino731)\
**Post date:** [April 26, 2015, 8:30am UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/10 "2015-04-26T08:30:53Z")

</div>

if you are very low health you can run away from the enemy by using the flag to running out of two minutes.

here the example code

```python

loop{
var enemy = this.findNearestEnemy();
var flag = this.findFlag("green");

if(flag){
    this.pickUpFlag(flag); 
    var pos = flag.pos;
    var x = pos.x;
    var y = pos.y;
    this.moveXY(x, y);
}

if(enemy && enemy.type != "sand-yak"){
    var dis = this.distanceTo(enemy);
    if(this.isReady("cleave")){
    this.cleave(enemy);
    }  
    else {
        this.attack(enemy);
    }
}
else {
    this.moveXY(91, 71);
}
}

```

---

<div class="post-metadata">

**Author:** ![spanishimp2](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@spanishimp2](https://discourse.codecombat.com/u/spanishimp2)\
**Post date:** [April 26, 2015, 9:39am UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/11 "2015-04-26T09:39:16Z")

</div>

I had the same problem i used a selfwait for 1 sec and then the code found the enemies I think they need time to appear

---

<div class="post-metadata">

**Author:** ![truffeb](https://avatars.discourse-cdn.com/v4/letter/t/3be4f8/32.png) [@truffeb](https://discourse.codecombat.com/u/truffeb)\
**Post date:** [March 20, 2016, 3:35pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/12 "2016-03-20T15:35:54Z")

</div>

Hello, I have a problem with my code. My hero does not attack the office, I do not understand why ?

```
loop:

enemy = self.findNearest(self.findEnemies())
    if enemy:
        if enemy.type is "sand-yak":
            pass
        else:
            if enemy.health>0:
                if self.cast("chain-lightning", enemy):
                    self.cast("chain-lightning", enemy)
                if self.isReady("bash"):
                    self.bash(enemy)
                if self.canElectrocute(enemy):
                    self.electrocute(enemy)
                else:
                    self.attack(enemy)
    self.moveXY(63, 72)
```

---

<div class="post-metadata">

**Author:** ![Kurniawan\_P](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/kurniawan_p/32/4314_2.png) [@Kurniawan\_P](https://discourse.codecombat.com/u/Kurniawan_P)\
**Post date:** [July 28, 2016, 4:21am UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/13 "2016-07-28T04:21:31Z")

</div>

i used the flags to retreat as you said, but my hero ignored the flag!

---

<div class="post-metadata">

**Author:** ![Mr\_NotSoPerfect](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@Mr\_NotSoPerfect](https://discourse.codecombat.com/u/Mr_NotSoPerfect)\
**Post date:** [January 6, 2017, 3:45pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/14 "2017-01-06T15:45:54Z")

</div>

Here’s my code (Python) my hero has 1475 hp but ends with 755 hp.

while True:  
enemy = hero.findNearestEnemy()  
if enemy and enemy.type != “sand-yak”:  
hero.attack(enemy)  
else:  
hero.shield()

---

<div class="post-metadata">

**Author:** ![Emblu056](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/emblu056/32/5761_2.png) [@Emblu056](https://discourse.codecombat.com/u/Emblu056)\
**Post date:** [January 17, 2017, 5:24pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/15 "2017-01-17T17:24:55Z")

</div>

@Mr_NotSoPerfect, how do you end with 755 hp? Is it -755? Or 755 hp? If it is 755 with out the minus, it might just be a glitch.

---

<div class="post-metadata">

**Author:** ![Mr\_NotSoPerfect](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@Mr\_NotSoPerfect](https://discourse.codecombat.com/u/Mr_NotSoPerfect)\
**Post date:** [January 23, 2017, 3:20pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/16 "2017-01-23T15:20:17Z")

</div>

I checked back at the code, and hero, my hero actually has 1471, and im guessing its the second round (the title has a small 1 next to it) and it says success, with 991 hp left & 1 enemy left too.

Hero:  
 ![](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex016/uploads/codecombat/original/2X/c/cf399fa159bfd32af70eab65b83652bf8c16a6f5.jpg)

Code:

while True:  
enemy = hero.findNearestEnemy()  
if enemy and enemy.type != “sand-yak”:  
hero.attack(enemy)  
else:  
hero.shield()

Hopefully this answers your question! If not reply back to me and I will check for any errors that you find.

---

<div class="post-metadata">

**Author:** ![Mr\_NotSoPerfect](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@Mr\_NotSoPerfect](https://discourse.codecombat.com/u/Mr_NotSoPerfect)\
**Post date:** [January 23, 2017, 3:52pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/18 "2017-01-23T15:52:20Z")

</div>

@Emblu056 I cant submit the run because it has not been a day yet, but here is my hero and code and i ran it twice and both times it said it was successful. Your hero does run into the wall a bunch of times, but the code works.

Hero:  
 ![](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex016/uploads/codecombat/original/2X/1/12dabe70b42f99a034e767cb00a934be0276b744.jpg)  
Code:  
while True:  
enemies = self.findEnemies()  
enemyIndex = 0  
while enemyIndex \< len(enemies):  
Target = enemies[enemyIndex]  
enemyIndex += 1  
enemy = self.findNearestEnemy()  
if enemy.type == “sand-yak”:  
self.moveXY(self.pos.x -5, self.pos.y)  
if enemy.type != “sand-yak”:  
self.attack(enemy)  
else:  
self.attack(enemy)

---

<div class="post-metadata">

**Author:** ![Emblu056](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/emblu056/32/5761_2.png) [@Emblu056](https://discourse.codecombat.com/u/Emblu056)\
**Post date:** [February 8, 2017, 2:35am UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/19 "2017-02-08T02:35:41Z")

</div>

@Mr_NotSoPerfect I tried both codes and they both didn’t work. Maybe you need to cleave. Try: self. cleave(enemy). If you have the sword. For the last code you gave me, the hero did nothing and just stood there.

---

<div class="post-metadata">

**Author:** ![Mr\_NotSoPerfect](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@Mr\_NotSoPerfect](https://discourse.codecombat.com/u/Mr_NotSoPerfect)\
**Post date:** [February 8, 2017, 1:38pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/20 "2017-02-08T13:38:32Z")

</div>

@Emblu056 did you put in the indents? and what does your hero look like?

---

<div class="post-metadata">

**Author:** ![Mr\_NotSoPerfect](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@Mr\_NotSoPerfect](https://discourse.codecombat.com/u/Mr_NotSoPerfect)\
**Post date:** [February 24, 2017, 2:08pm UTC](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222/21 "2017-02-24T14:08:36Z")

</div>

move enemy = self.findNearestEnemy() in the while enemyindex \< 100

so like this:

loop:  
enemy = self.findNearestEnemy()  
enemyindex = 0  
while enemyindex \< 100:  
enemy = self.findNearestEnemy()

[Next page](https://discourse.codecombat.com/t/sarven-desert-help-with-sarven-brawl-using-the-python-coding-language/3222.md?page=2)
