# Help Sand snakes!

**URL:** <https://discourse.codecombat.com/t/help-sand-snakes/23475>\
**Category:** Level Help\
**Created:** [August 12, 2020, 1:08pm UTC](https://discourse.codecombat.com/t/help-sand-snakes/23475 "2020-08-12T13:08:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![CODEBOY2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/codeboy2/32/13507_2.png) [@CODEBOY2](https://discourse.codecombat.com/u/CODEBOY2)\
**Post date:** [August 12, 2020, 1:08pm UTC](https://discourse.codecombat.com/t/help-sand-snakes/23475/1 "2020-08-12T13:08:45Z")

</div>

```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.

while True:
    coins = hero.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 = hero.distanceTo(coin)
        # If this coin's distance is less than the nearestDistance
        if (hero.distanceTo(coin) < nearestDistance):
            # Set nearest to coin
            nearestDistance = coin
            # Set nearestDistance to distance
            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.

```

i dont get this part↑↑↑

---

<div class="post-metadata">

**Author:** ![DimaP](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dimap/32/10613_2.png) [@DimaP](https://discourse.codecombat.com/u/DimaP)\
**Post date:** [August 12, 2020, 1:18pm UTC](https://discourse.codecombat.com/t/help-sand-snakes/23475/2 "2020-08-12T13:18:57Z")

</div>

I think, that you have to put `coinIndex` to the end of the loop.

---

<div class="post-metadata">

**Author:** ![CODEBOY2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/codeboy2/32/13507_2.png) [@CODEBOY2](https://discourse.codecombat.com/u/CODEBOY2)\
**Post date:** [August 12, 2020, 1:20pm UTC](https://discourse.codecombat.com/t/help-sand-snakes/23475/3 "2020-08-12T13:20:00Z")

</div>

what do you mean?(20 chars)

---

<div class="post-metadata">

**Author:** ![DimaP](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dimap/32/10613_2.png) [@DimaP](https://discourse.codecombat.com/u/DimaP)\
**Post date:** [August 12, 2020, 1:20pm UTC](https://discourse.codecombat.com/t/help-sand-snakes/23475/4 "2020-08-12T13:20:11Z")

</div>

Sorry, I mean `coinIndex+=1` to the end of the loop.

---

<div class="post-metadata">

**Author:** ![DimaP](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dimap/32/10613_2.png) [@DimaP](https://discourse.codecombat.com/u/DimaP)\
**Post date:** [August 12, 2020, 1:21pm UTC](https://discourse.codecombat.com/t/help-sand-snakes/23475/5 "2020-08-12T13:21:22Z")

</div>

> [@CODEBOY2](#):
>
> ` nearestDistance = coin`

I think, you should write

```python
nearest = coin

```

---

<div class="post-metadata">

**Author:** ![CODEBOY2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/codeboy2/32/13507_2.png) [@CODEBOY2](https://discourse.codecombat.com/u/CODEBOY2)\
**Post date:** [August 12, 2020, 1:21pm UTC](https://discourse.codecombat.com/t/help-sand-snakes/23475/6 "2020-08-12T13:21:45Z")

</div>

wil try okkkkkkkkkkkkkkkkkk

---

<div class="post-metadata">

**Author:** ![CODEBOY2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/codeboy2/32/13507_2.png) [@CODEBOY2](https://discourse.codecombat.com/u/CODEBOY2)\
**Post date:** [August 12, 2020, 1:22pm UTC](https://discourse.codecombat.com/t/help-sand-snakes/23475/7 "2020-08-12T13:22:36Z")

</div>

but i dont know what to do at the end of the code

---

<div class="post-metadata">

**Author:** ![DimaP](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dimap/32/10613_2.png) [@DimaP](https://discourse.codecombat.com/u/DimaP)\
**Post date:** [August 12, 2020, 1:24pm UTC](https://discourse.codecombat.com/t/help-sand-snakes/23475/8 "2020-08-12T13:24:01Z")

</div>

> [@CODEBOY2](#):
>
> `# If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.`

After this, you should write `hero.moveXY(nearest.pos.x,nearest.pos.y)`

---

<div class="post-metadata">

**Author:** ![CODEBOY2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/codeboy2/32/13507_2.png) [@CODEBOY2](https://discourse.codecombat.com/u/CODEBOY2)\
**Post date:** [August 12, 2020, 1:25pm UTC](https://discourse.codecombat.com/t/help-sand-snakes/23475/9 "2020-08-12T13:25:09Z")

</div>

> [@DimaP](#):
>
> hero.moveXY(nearest.pos.x,nearest.pos.y)

thanks it works!!! 😁 😁 😁

---

<div class="post-metadata">

**Author:** ![DimaP](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dimap/32/10613_2.png) [@DimaP](https://discourse.codecombat.com/u/DimaP)\
**Post date:** [August 12, 2020, 1:26pm UTC](https://discourse.codecombat.com/t/help-sand-snakes/23475/10 "2020-08-12T13:26:20Z")

</div>

You’re welcome. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [August 13, 2020, 1:26am UTC](https://discourse.codecombat.com/t/help-sand-snakes/23475/11 "2020-08-13T01:26:21Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
