# \[SOLVED\] Sand Snakes Level Problem (Python)

**URL:** <https://discourse.codecombat.com/t/solved-sand-snakes-level-problem-python/4335>\
**Category:** Level Help\
**Created:** [June 26, 2015, 11:43am UTC](https://discourse.codecombat.com/t/solved-sand-snakes-level-problem-python/4335 "2015-06-26T11:43:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vollut](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vollut/32/2786_2.png) [@Vollut](https://discourse.codecombat.com/u/Vollut)\
**Post date:** [June 26, 2015, 11:43am UTC](https://discourse.codecombat.com/t/solved-sand-snakes-level-problem-python/4335/1 "2015-06-26T11:43:05Z")

</div>

Hey! I’m really stuck with this level. Here is my code:

```python
loop:
    coins = self.findItems()
    coinIndex = 0
    nearest = None
    nearestDistance = 9999
    
    while coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        distance = self.distanceTo(coin)
 
    if distance < nearestDistance:
        nearest = coin
        nearestDistance = distance
    if nearest:
        self.moveXY(nearest.pos.x, nearest.pos.y)

```

My hero just runs into the mine. That’s the problem?

---

<div class="post-metadata">

**Author:** ![ant](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ant/32/3362_2.png) [@ant](https://discourse.codecombat.com/u/ant)\
**Post date:** [June 26, 2015, 1:31pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-level-problem-python/4335/2 "2015-06-26T13:31:37Z")

</div>

Hello @Vollut and welcome to the forums! Feel free to reuse existing topics (related to this level) to ask your question next time.

* * *

What your code currently does is that it goes through the list of coins and calculates their distance. Once that is finished(!), it compares _distance_ with _nearestDistance_. However, at this point _distance_ holds the value of the distance of the last coin…

So what do you need to fix it?

You should check the distance **inside** the `while` block, so you check it for **every** coin, not only the last one…

```python
loop:
# define variables
    while ...
        # get coin distance
        if distance < nearestDistance:
            # set nearestCoin and nearestDistance
    if nearest:
        # move to coin

```

---

<div class="post-metadata">

**Author:** ![Vollut](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vollut/32/2786_2.png) [@Vollut](https://discourse.codecombat.com/u/Vollut)\
**Post date:** [June 26, 2015, 1:48pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-level-problem-python/4335/3 "2015-06-26T13:48:16Z")

</div>

Oh, now I get it! Thank you! Now it works

---

<div class="post-metadata">

**Author:** ![Sirtrumpet](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sirtrumpet/32/9060_2.png) [@Sirtrumpet](https://discourse.codecombat.com/u/Sirtrumpet)\
**Post date:** [November 8, 2018, 10:14pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-level-problem-python/4335/4 "2018-11-08T22:14:48Z")

</div>

How does this not work.

```python
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 coin.distance < nearestDistance:
            
            # Set nearest to coin
            nearest = coin
            # Set nearestDistance to distance
            nearestDistance = distance
            hero.moveXY(nearest.pos.x, nearest.pos.y)
    # If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap
    hero.moveXY(nearest.pos.x, nearest.pos.y)

```

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [November 9, 2018, 1:46am UTC](https://discourse.codecombat.com/t/solved-sand-snakes-level-problem-python/4335/5 "2018-11-09T01:46:02Z")

</div>

First, the line after `nearestDistance = distance` shouldn’t be there. You’re telling your hero to move twice.

> [@Sirtrumpet](#):
>
> ```python
> # If there's a nearest coin, move to its position.
> 
> ```

You’ve defined the variable, “nearest,” but you’re not checking to see **`if nearest:`** before trying to move there. You should always check to make sure that an object exists before acting on the object. In this case it should work because nearest always exists but don’t count on that. Just get in the habit of putting if statements first.

```python
if object:
    do something with object

```

---

<div class="post-metadata">

**Author:** ![GangstaPandaRKR](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gangstapandarkr/32/11198_2.png) [@GangstaPandaRKR](https://discourse.codecombat.com/u/GangstaPandaRKR)\
**Post date:** [January 10, 2020, 5:53pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-level-problem-python/4335/6 "2020-01-10T17:53:26Z")

</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 coin.distance < nearestDistance:
            # Set nearest to coin
            nearest = 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.
    if nearest:
        hero.moveXY(nearest.pos.x, nearest.pos.y)

```

help 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:** [January 11, 2020, 11:02am UTC](https://discourse.codecombat.com/t/solved-sand-snakes-level-problem-python/4335/7 "2020-01-11T11:02:22Z")

</div>

> [@GangstaPandaRKR](#):
>
> ```python
> if coin.distance < nearestDistance:
> 
> ```

What’s coin.distance? Why don’t you use the variable you just defined on the line above.  
When you set a variable like you did there: `distance = hero.distanceTo(coin)` It doesn’t become a property of coin, like coin.distance, instead it’s completely separate from the “coin”.  
Danny

---

<div class="post-metadata">

**Author:** ![GangstaPandaRKR](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gangstapandarkr/32/11198_2.png) [@GangstaPandaRKR](https://discourse.codecombat.com/u/GangstaPandaRKR)\
**Post date:** [January 11, 2020, 8:40pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-level-problem-python/4335/8 "2020-01-11T20:40:20Z")

</div>

Thank you! I finished it.

---

<div class="post-metadata">

**Author:** ![HatKid22](https://avatars.discourse-cdn.com/v4/letter/h/8e8cbc/32.png) [@HatKid22](https://discourse.codecombat.com/u/HatKid22)\
**Post date:** [October 24, 2020, 1:13pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-level-problem-python/4335/9 "2020-10-24T13:13:47Z")

</div>

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 coin.distance < nearestDistance:
        coin.distance = nearestDistance
        nearest = 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.
if nearest:
    hero.moveXY(nearest.pos.x, nearest.pos.y)

```

What am I doing wrong?

---

<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 24, 2020, 4:40pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-level-problem-python/4335/10 "2020-10-24T16:40:01Z")

</div>

Hi @HatKid22, welcome to the forum. Your code is not fully formatted.  
Please read this topic to learn how to format your code:

> [@\[Essentials\] How To Post/Format Your Code Correctly](https://discourse.codecombat.com/t/essentials-how-to-post-format-your-code-correctly/15521):
>
> Formatting your code before posting code is essential to our community, and if you don’t we might not be able to help frowning Everyone must know how to format ALL of your code properly. It’s really easy and only requires a very small amount of effort. To post your code from the game , use the \</\> button or it won’t format properly. When you click the \</\> button, the following will appear: Please paste ALL …

Thanks  
Danny

---

<div class="post-metadata">

**Author:** ![LunaFromTheMoon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lunafromthemoon/32/37747_2.png) [@LunaFromTheMoon](https://discourse.codecombat.com/u/LunaFromTheMoon)\
**Post date:** [November 17, 2020, 3:08pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-level-problem-python/4335/11 "2020-11-17T15:08:01Z")

</div>

if coin.distance \< nearestDistance:  
coin.distance does not exist, try using something else.

Luna
