# The Trials - Python Help

**URL:** <https://discourse.codecombat.com/t/the-trials-python-help/6599>\
**Category:** Level Help\
**Created:** [January 27, 2016, 3:57pm UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599 "2016-01-27T15:57:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jacob\_Kohler](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@Jacob\_Kohler](https://discourse.codecombat.com/u/Jacob_Kohler)\
**Post date:** [January 27, 2016, 3:57pm UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/1 "2016-01-27T15:57:58Z")

</div>

I have been trying to solve The Trials in parts. This is my code for getting to the first oasis. It was working but now it is giving me the error that “distanceTo target is null”. Any help with how to solve this would be great.  
`loop: enemies = self.findEnemies() nearest = self.findNearest(enemies) distance = self.distanceTo(nearest) if distance: if distance <= 25: self.attack(nearest) else: self.move({"x": 135, "y": 15})`

---

<div class="post-metadata">

**Author:** ![Steven\_Kang2003](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@Steven\_Kang2003](https://discourse.codecombat.com/u/Steven_Kang2003)\
**Post date:** [January 27, 2016, 4:33pm UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/2 "2016-01-27T16:33:04Z")

</div>

plz format

```python
self.say("plz format")

```

---

<div class="post-metadata">

**Author:** ![AdrianCgw](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@AdrianCgw](https://discourse.codecombat.com/u/AdrianCgw)\
**Post date:** [January 27, 2016, 11:57pm UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/4 "2016-01-27T23:57:43Z")

</div>

Most of the functions will give generate an error if you do not give them a valid argument.  
The error:  
"distanceTo target is null"  
says that the value nearest used to call:

```python
distance = self.distanceTo(nearest)

```

is null.

This will happen if there are no enemies near your hero.

Do a test beforehand to make sure you have an nearest enemy.  
I actually like that you were checking iif the distance is valid, but the error was occurring before that

```python
loop:
    enemies = self.findEnemies()
    nearest = self.findNearest(enemies)
    if (nearest)
        distance = self.distanceTo(nearest)
            if distance <= 25:
                self.attack(nearest)
            else:
                self.move({"x": 135, "y": 15})

```

---

<div class="post-metadata">

**Author:** ![Jacob\_Kohler](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@Jacob\_Kohler](https://discourse.codecombat.com/u/Jacob_Kohler)\
**Post date:** [January 28, 2016, 3:20am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/5 "2016-01-28T03:20:41Z")

</div>

Thats what I needed. Thank you!

---

<div class="post-metadata">

**Author:** ![Jacob\_Kohler](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@Jacob\_Kohler](https://discourse.codecombat.com/u/Jacob_Kohler)\
**Post date:** [January 28, 2016, 3:28am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/6 "2016-01-28T03:28:27Z")

</div>

How do I indent my code?

---

<div class="post-metadata">

**Author:** ![AdrianCgw](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@AdrianCgw](https://discourse.codecombat.com/u/AdrianCgw)\
**Post date:** [January 28, 2016, 7:17am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/7 "2016-01-28T07:17:15Z")

</div>

put 3 backquotes (left-top symbol on your keyboard on a empty line before and after your code)

```python

```

your\_Code

```python

```

---

<div class="post-metadata">

**Author:** ![Finnian\_Weiland](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/finnian_weiland/32/10976_2.png) [@Finnian\_Weiland](https://discourse.codecombat.com/u/Finnian_Weiland)\
**Post date:** [November 13, 2019, 11:17pm UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/8 "2019-11-13T23:17:37Z")

</div>

can you please give me some help i don’t have any code

---

<div class="post-metadata">

**Author:** ![Finnian\_Weiland](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/finnian_weiland/32/10976_2.png) [@Finnian\_Weiland](https://discourse.codecombat.com/u/Finnian_Weiland)\
**Post date:** [November 13, 2019, 11:19pm UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/9 "2019-11-13T23:19:33Z")

</div>

please please please!!!

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [November 14, 2019, 10:59am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/10 "2019-11-14T10:59:51Z")

</div>

Calm down. Saying please three times isn’t going to help.  
Try and write some code, you could use flags, hero.time, hero.moveXY(), special abilities, etc…  
This is an optional challenge level, so it’s quite hard. You’ll have to put a bit more effort in before I give you proper help.  
Danny

---

<div class="post-metadata">

**Author:** ![wazi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wazi/32/14115_2.png) [@wazi](https://discourse.codecombat.com/u/wazi)\
**Post date:** [October 8, 2020, 10:06am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/11 "2020-10-08T10:06:58Z")

</div>

did I make a mistake in this if so can you please help me

```python
enemy = hero.findNearestEnemy()
enemyMissiles = hero.findEnemyMissiles()
hazards = hero.findHazards()
hero.findNearest(hero.findEnemies())
enemies = hero.findEnemies()
for enemy in enemies:
    while True:
        if enemy:
            if enemy.type != "sand-yak":
                hero.attack(enemy)
            if hazards or enemyMissiles:
                hero.shield()
            if hero.health < 400:
                # Health is low–retreat!
                while True:
                    hero.shield()
            else:
                # Health is fine. Fight!
                hero.attack(enemy)
```

---

<div class="post-metadata">

**Author:** ![wazi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wazi/32/14115_2.png) [@wazi](https://discourse.codecombat.com/u/wazi)\
**Post date:** [October 8, 2020, 10:07am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/12 "2020-10-08T10:07:58Z")

</div>

I really need help so can you please help me

---

<div class="post-metadata">

**Author:** ![PeterPalov](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/peterpalov/32/45280_2.png) [@PeterPalov](https://discourse.codecombat.com/u/PeterPalov)\
**Post date:** [October 8, 2020, 10:32am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/13 "2020-10-08T10:32:19Z")

</div>

> [@wazi](#):
>
> `for enemy in enemies:`

Do we learn “for” in Desert? Is it your code?

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [October 8, 2020, 10:34am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/14 "2020-10-08T10:34:20Z")

</div>

`for` loops is officially covered in the mountains, not the desert. (Programmaticon IV)

---

<div class="post-metadata">

**Author:** ![PeterPalov](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/peterpalov/32/45280_2.png) [@PeterPalov](https://discourse.codecombat.com/u/PeterPalov)\
**Post date:** [October 8, 2020, 10:35am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/15 "2020-10-08T10:35:15Z")

</div>

Yes. So that’s why I ask wazi about code.

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [October 8, 2020, 10:36am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/16 "2020-10-08T10:36:15Z")

</div>

Since The Trials is an **optional** subscriber challenge level, he might’ve learned `for` loops already.

---

<div class="post-metadata">

**Author:** ![PeterPalov](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/peterpalov/32/45280_2.png) [@PeterPalov](https://discourse.codecombat.com/u/PeterPalov)\
**Post date:** [October 8, 2020, 10:38am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/17 "2020-10-08T10:38:40Z")

</div>

Okay.  
So let’s help wazi.

---

<div class="post-metadata">

**Author:** ![PeterPalov](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/peterpalov/32/45280_2.png) [@PeterPalov](https://discourse.codecombat.com/u/PeterPalov)\
**Post date:** [October 8, 2020, 10:39am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/18 "2020-10-08T10:39:46Z")

</div>

In this level I used flags and collected mushrooms (items).  
Also, try to put al these things:

> [@wazi](#):
>
> ```python
> enemy = hero.findNearestEnemy()
> enemyMissiles = hero.findEnemyMissiles()
> hazards = hero.findHazards()
> hero.findNearest(hero.findEnemies())
> enemies = hero.findEnemies()
> 
> ```

to the While True loop.

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [October 8, 2020, 7:45pm UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/19 "2020-10-08T19:45:28Z")

</div>

> [@wazi](#):
>
> `enemies = hero.findEnemies()`

Why do you need this if you already have

> [@wazi](#):
>
> `enemy = hero.findNearestEnemy()`

.

> [@wazi](#):
>
> ```python
> enemyMissiles = hero.findEnemyMissiles()
> hazards = hero.findHazards()
> 
> ```

I do not think that you will need those in this level.

All the variables you need for this level is:

```python
enemy = hero.findNearestEnemy()
flag = hero.findFlag("black")
item = hero.findNearestItem()

```

The flag and item are optional, but you’ll need moveXY if you don’t use the flag. I find the flag helpful in this level.  
I completed the level with Naria. This is a level where you’ll need good strategy or good armor.  
Please send a screenshot of your armor.  
Thanks, Lydia

---

<div class="post-metadata">

**Author:** ![wazi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wazi/32/14115_2.png) [@wazi](https://discourse.codecombat.com/u/wazi)\
**Post date:** [October 9, 2020, 9:42am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/20 "2020-10-09T09:42:11Z")

</div>

sorry about the

```python
enemy = hero.findNearestEnemy()

```

and the

```python
enemies = hero.findEnemies()

```

I’ll fix my code

---

<div class="post-metadata">

**Author:** ![wazi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wazi/32/14115_2.png) [@wazi](https://discourse.codecombat.com/u/wazi)\
**Post date:** [October 9, 2020, 9:43am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/21 "2020-10-09T09:43:25Z")

</div>

There it goes

```python
enemy = hero.findNearestEnemy()
enemyMissiles = hero.findEnemyMissiles()
hazards = hero.findHazards()
hero.findNearest(hero.findEnemies())
item = hero.findNearestItem()
for enemy in enemies:
    while True:
        if enemy:
            if enemy.type != "sand-yak":
                hero.attack(enemy)
            if hazards or enemyMissiles:
                hero.shield()
            if hero.health < 400:
                # Health is low–retreat!
                while True:
                    hero.shield()
            else:
                # Health is fine. Fight!
                hero.attack(enemy)

```

[Next page](https://discourse.codecombat.com/t/the-trials-python-help/6599.md?page=2)
