# 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:** 12\
**Page:** 2

<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:59am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/23 "2020-10-09T09:59:41Z")

</div>

There it is  
 ![FireShot Capture 060 - Play CodeCombat Levels - Learn Python, JavaScript, and HTML -CodeCom_ - codecombat.com324x433, 100%](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/3/311685eb6877cf61309576d20d764851158c4fa3.png)

---

<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 9, 2020, 10:00am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/24 "2020-10-09T10:00:22Z")

</div>

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:** ![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, 10:20am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/25 "2020-10-09T10:20:28Z")

</div>

okay I’ve change my code

```python
enemy = hero.findNearestEnemy()
if len(hero.findFriends()) > 5:
    hero.shield()
else:
    hero.attack(enemy)
if len(hero.findFriends()) > 5:
    hero.shield()
else:
    hero.attack(enemy)
66.67
enemy = hero.findNearestEnemy()
if enemy:
    # Use if to make sure enemy exists before checking its distance.
    distance = hero.distanceTo(enemy)
    if distance < 10:
        hero.attack(enemy)
    else:
        hero.say("Come closer!")
hero.findByType("thrower", hero.findFriends())
enemies = hero.findEnemies()
for enemy in enemies:
    # Do something with each enemy here
    hero.attack(enemy) # Example
friends = hero.findFriends()
for friend in friends:
    # Do something with each friend here
    hero.follow(friend)
items = hero.findItems()
for item in items:
    # Do something with each item here
    hero.moveXY(item.pos.x, item.pos.y)
hero.findNearest(hero.findItems())
enemy = hero.findNearestEnemy()
hero.attack(enemy)
hero.attack(enemy)
item = hero.findNearestItem()
if item:
    # Move to the position of the item.
    pos = item.pos
    x = pos.x
    y = pos.y
    hero.moveXY(x, y)
item = hero.findNearest(hero.findItems())
if item and hero.isPathClear(hero.pos, hero.pos):
    hero.move(item.pos)
else:
    hero.move({"x": hero.pos.x, "y": hero.pos.y + 5})
if hero.health < 200:
    # Health is low–retreat!
    hero.moveXY(61, 46)
else:
    # Health is fine. Fight!
    hero.attack(enemy)
1521.30
if hero.health < hero.maxHealth / 3:
    # Health is low–retreat!
    hero.moveXY(61, 46)
else:
    # Health is fine. Fight!
    hero.attack(enemy)
5121.30
# Move 10 meters to the right.
ne.wX = hero.pos.x + 10
newY = hero.pos.y
moveXY(newX, newY)
{x: 23.59, y: 18.31,z: 1.00}
if hero.gold > 100:
    hero.say("I'm rich!")
null
if hero.isReady("cleave"):
    hero.cleave(enemy)
else:
    hero.attack(enemy)
if hero.time < 30:
    hero.say("Wait...")
hero.wait(0.1)
hero.moveXY(24, 35)
hero.canElectrocute(hero.findNearestEnemy())
hero.electrocute(hero.findNearestEnemy())
flag = hero.findFlag()
if flag:
    if flag.color is "black":
        hero.buildXY("fire-trap", flag.pos.x, flag.pos.y)
    hero.pickUpFlag(flag)
greenFlag = hero.findFlag("green")
if greenFlag:
    hero.pickUpFlag(greenFlag)
arr = ["this is the first element", "this is the second element"]
hero.say(arr[0]) # says "this is the first element"
hero.say(arr[1]) # says "this is the second element"
hero.say(arr[len(arr) - 1]) # says "this is the second element

arr[0] = 'new first element'
arr.append('a third element') # append() adds an element at the end

lastElement = arr[-1] # elements can be referenced from the end of the list using negative index values   
while True:
    enemy = hero.findNearest(hero.findEnemies())
    if not enemy:
        break
    hero.attack(enemy)
hero.say("／人 ◕ ‿‿ ◕ 人＼")
if hero.isReady("cleave"):
    hero.cleave(enemy)
else:
    hero.attack(enemy)
# Example: if you see an enemy, then attack it.
enemy = hero.findNearestEnemy()
if enemy:
    hero.attack(enemy)

# More examples
enemy = hero.findNearestEnemy()
if not enemy:
    # If there is *not* an enemy, then move...
    hero.moveXY(30, 30)
elif enemy.type is "thrower":
    # ... else if the enemy's type *equals* "thrower", then attack...
    hero.attack(enemy)
elif hero.isReady("cleave") and hero.distanceTo(enemy) < 10:
    # ... else if "cleave" is ready *and* the distance is *less than* 10m, then cleave...
    hero.cleave(enemy)
else:
    # ... else, shield.
    hero.shield()
i = 10
while i >= 0:
    hero.say("Counted to " + i)
    i -= 1
# Example: looping through a maze.
while True:
    hero.moveRight()
    hero.moveDown()
    hero.moveRight()
    hero.moveUp()

# Example: attack an enemy over and over.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
hero.say("Hi!")

```

---

<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, 10:22am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/26 "2020-10-09T10:22:59Z")

</div>

my hero keeps on saying “but it’s dead” and I have too less health  
my health is only 1521

---

<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 9, 2020, 10:25pm UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/27 "2020-10-09T22:25:18Z")

</div>

If you put `else` infront of `if`, the `if` won’t run at all.  
Lydia

---

<div class="post-metadata">

**Author:** ![Aditya\_Kulkarni\_WV](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/aditya_kulkarni_wv/32/22026_2.png) [@Aditya\_Kulkarni\_WV](https://discourse.codecombat.com/u/Aditya_Kulkarni_WV)\
**Post date:** [October 19, 2021, 3:26am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/28 "2021-10-19T03:26:11Z")

</div>

i need help in my code

---

<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:** [October 19, 2021, 7:31am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/29 "2021-10-19T07:31:59Z")

</div>

If you need help then please post your code into the forum from the game, and paste it inbetween two lines of ``` because that will format the code.  
Danny

---

<div class="post-metadata">

**Author:** ![escaper10101](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/escaper10101/32/31713_2.png) [@escaper10101](https://discourse.codecombat.com/u/escaper10101)\
**Post date:** [October 22, 2021, 5:51am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/30 "2021-10-22T05:51:41Z")

</div>

Wazi, your code is fine but there is a big mistake,

> [@wazi](#):
>
> ```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)
> 
> ```

Firstly, these and the for loop should be inside the while true loop.

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

Secondly,

> [@wazi](#):
>
> ```python
> if hazards or enemyMissiles:
> hero.shield()
> 
> ```

Once you see a long-range enemy’s missile you will stop attacking and keep shielding but nobody is going to help you defeat the enemy.  
Thirdly, you are not collecting mushrooms, btw mushrooms in this level can heal you.  
Also you can try using flags.  
Hope this helps. Oh, and are you a sub, wazi?

---

<div class="post-metadata">

**Author:** ![escaper10101](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/escaper10101/32/31713_2.png) [@escaper10101](https://discourse.codecombat.com/u/escaper10101)\
**Post date:** [October 22, 2021, 5:52am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/31 "2021-10-22T05:52:50Z")

</div>

If you are a sub, you can gain access to 50000 gems at first to buy better armor as health is really important at this level.

---

<div class="post-metadata">

**Author:** ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)\
**Post date:** [October 22, 2021, 12:49pm UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/32 "2021-10-22T12:49:46Z")

</div>

> [@escaper10101](#):
>
> 50000

Actually its 43 000  
20 characters

---

<div class="post-metadata">

**Author:** ![escaper10101](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/escaper10101/32/31713_2.png) [@escaper10101](https://discourse.codecombat.com/u/escaper10101)\
**Post date:** [October 23, 2021, 3:25am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/33 "2021-10-23T03:25:03Z")

</div>

My new account gained me 50000 gems though Eric\_Tang so I believe it is 50000 gems.

---

<div class="post-metadata">

**Author:** ![escaper10101](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/escaper10101/32/31713_2.png) [@escaper10101](https://discourse.codecombat.com/u/escaper10101)\
**Post date:** [October 23, 2021, 3:25am UTC](https://discourse.codecombat.com/t/the-trials-python-help/6599/34 "2021-10-23T03:25:32Z")

</div>

Did you do some permanent spending or stuff?

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