# Sand Snakes-Sarven Desert

**URL:** <https://discourse.codecombat.com/t/sand-snakes-sarven-desert/5224>\
**Category:** Uncategorized\
**Created:** [October 13, 2015, 1:27am UTC](https://discourse.codecombat.com/t/sand-snakes-sarven-desert/5224 "2015-10-13T01:27:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Wojciechowski](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@Mark\_Wojciechowski](https://discourse.codecombat.com/u/Mark_Wojciechowski)\
**Post date:** [October 13, 2015, 1:27am UTC](https://discourse.codecombat.com/t/sand-snakes-sarven-desert/5224/1 "2015-10-13T01:27:38Z")

</div>

I’m not sure how to avoid the traps…any ideas, friends?

```python
# This field is covered in firetraps. Thankfully we've sent a scout ahead to find a path. He left coins along the path so that if we always stick to the nearest coin, we'll avoid the traps.

# This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.

loop:
    coins = self.findItems()
    coinIndex = 0
    nearest = None 
    nearestDistance = 9999
    
    # Loop through all the coins to find the nearest one.
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        distance = self.distanceTo(coin)
        # If this coin's distance is less than the nearestDistance
            # Set nearest to coin
            # Set nearestDistance to distance
        if distance < nearestDistance:
            nearest = coin
            nearestDistance = distance
    # If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
            if coin:
                self.moveXY(coin.pos.x, coin.pos.y)

```

---

<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 13, 2015, 1:35am UTC](https://discourse.codecombat.com/t/sand-snakes-sarven-desert/5224/2 "2015-10-13T01:35:12Z")

</div>

Put the part where you move to the nearest coin outside the while-loop. Otherwise you’ll just move to the coin you’re checking.
