# \[Solved\] Sand snakes (Python)

**URL:** <https://discourse.codecombat.com/t/solved-sand-snakes-python/29795>\
**Category:** Level Help\
**Created:** [July 27, 2021, 10:00pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795 "2021-07-27T22:00:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [July 27, 2021, 10:00pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/1 "2021-07-27T22:00:50Z")

</div>

Hi, I am struggling on how to get started on this level. I have no idea what to do. This is the given code:

```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
        
            # Set nearest to coin
            
            # Set nearestDistance to 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.

```

Can someone give me ideas on how to begin?

---

<div class="post-metadata">

**Author:** ![RangerGrant9307](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/rangergrant9307/32/26001_2.png) [@RangerGrant9307](https://discourse.codecombat.com/u/RangerGrant9307)\
**Post date:** [July 27, 2021, 10:21pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/2 "2021-07-27T22:21:46Z")

</div>

OK heres the gist.  
Loop thru all the coins.  
If the coins distance to you is smaller then nearest distance.  
Make nearestDistance to the distance, and make nearest to the coin.  
So lets say you start.  
Coin A is close then 9999.  
So you pick it up.  
Then,since COIN A is gone, you get coinB.  
If its closer then 9999:  
Pick it up.  
Its kinda like finding the weakest enemy.  
You make weakest to none, and make weakest health to 9999  
Then, loop thru all enemies, and if its health is less then weakest, make it weakest.  
Lets say, there is an ogre, a shaman, and a thrower.  
120 is less then 9999 so weakestHealth is 120, and ogre is weakest. Then, you find the shaman in the loop.  
60 is less then 120, so weakestHealth is 60, and the shaman is weakest.  
Then, you find the thrower.  
7 is less then 60, so then weakestHealth is 7, and thrower is weakest.  
If the game spawns an enemy that’s health is less then 7, its weakest enemy.  
Here is some code for getting the weakest enemy, and it might give you some ideas for something similar for the NEAREST item.

```python
while True:
    weakest = None
    leastHealth = 99999
    enemies = hero.findEnemies()
    for enemy in enemies:
        if enemy.health < leastHealth:    
            weakest = enemy
            lowestHealth = enemy.health
    # Loop through all enemies.
        
    if weakest:
        hero.attack(weakest)

```

Then, for the items, just do it with distance, and do items instead of enemies.  
I hope this helps.

---

<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:** [July 27, 2021, 10:36pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/3 "2021-07-27T22:36:29Z")

</div>

> [@enPointe77](#):
>
> ```python
> # If this coin's distance is less than the nearestDistance
>         
> # Set nearest to coin
>             
> # Set nearestDistance to 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.
> 
> ```

This is pretty clear, just follow the instructions

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [July 28, 2021, 4:42am UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/4 "2021-07-28T04:42:19Z")

</div>

Thanks, I will try this tomorrow.  
@Lydia_Song, those instructions made me look like this 😶😐😵‍. They didn’t help me.

---

<div class="post-metadata">

**Author:** ![gotahugesword](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gotahugesword/32/23355_2.png) [@gotahugesword](https://discourse.codecombat.com/u/gotahugesword)\
**Post date:** [July 28, 2021, 4:48pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/5 "2021-07-28T16:48:39Z")

</div>

the instructions are basically telling you write code that finds out if the coin is the closest.

like imagine coinA, coinB, and coinC  
coinA is 5 m away, coinB is 3 m away, and coinC is 13 m away

you have to cycle through every coin and see which is the closest.  
nearestDistance = 999  
nearest = 0  
coinA distance \< nearestDistance so coinA distance becomes the new nearest distance and coinA becomes nearest

nearestDistance = 5

coinB distance \< nearestDistance so coinB becomes nearest and repeat.

---

<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:** [July 28, 2021, 5:31pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/6 "2021-07-28T17:31:42Z")

</div>

So basically, you’re looping through all the coins to find the nearest coin.  
They gave you the nearest as None and nearestDistance at 9999 (cause its just a starting distance and every coin’s distance would be lower than that)

> [@enPointe77](#):
>
> ```python
> # If this coin's distance is less than the nearestDistance
>         
> # Set nearest to coin
>             
> # Set nearestDistance to 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.
> 
> ```

So here it’s telling you, if the distance to the coin is less than the nearestDistance, do `nearest = coin.` And do `nearestDistance = distance`.  
If there is a nearest, use moxeXY to move to the nearest coin

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [August 10, 2021, 10:01pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/7 "2021-08-10T22:01:34Z")

</div>

I’m still struggling on this code. I think I’ve got most of it, but there are two spots I can’t figure it out. Here’s my current code:

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

```

I don’t get what to do right before the given code, when it says ` You'll need to find the nearest coins on your own.` And I also don’t understand what to do when it says `# If this coin's distance is less than the nearestDistance `

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [August 10, 2021, 10:02pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/8 "2021-08-10T22:02:57Z")

</div>

Also, in this entire spot:

```python
 if item.distance < nearestDistance:
            nearest = item
            nearestDistance = item.distance

```

Should I write coin instead of item?

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [August 10, 2021, 10:53pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/9 "2021-08-10T22:53:31Z")

</div>

I switched it out with coins, and now my hero moves. She picks up 1 coin, and blows up.

```python
 This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.
hero.findItems()
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:
            nearest = coin
            nearestDistance = coin.distance
            # Set nearest to coin
            
            # Set nearestDistance to 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.
    hero.moveXY(coin.pos.x, coin.pos.y)

```

---

<div class="post-metadata">

**Author:** ![RangerGrant9307](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/rangergrant9307/32/26001_2.png) [@RangerGrant9307](https://discourse.codecombat.com/u/RangerGrant9307)\
**Post date:** [August 10, 2021, 10:54pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/10 "2021-08-10T22:54:49Z")

</div>

Just put your move command into the while loop. LOL

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [August 10, 2021, 10:56pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/11 "2021-08-10T22:56:37Z")

</div>

Like that?

```python
  while True:
        hero.moveXY(coin.pos.x, coin.pos.y)

```

My hero still blows up. So I’m not sure I fully understand what you’re trying to say.

---

<div class="post-metadata">

**Author:** ![RangerGrant9307](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/rangergrant9307/32/26001_2.png) [@RangerGrant9307](https://discourse.codecombat.com/u/RangerGrant9307)\
**Post date:** [August 10, 2021, 10:57pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/12 "2021-08-10T22:57:46Z")

</div>

```python
        if coin.distance < nearestDistance:
            nearest = coin
            nearestDistance = coin.distance
            moooooov

```

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [August 10, 2021, 11:00pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/13 "2021-08-10T23:00:16Z")

</div>

```python
        if coin.distance < nearestDistance:
            nearest = coin
            nearestDistance = coin.distance
            
            hero.moveXY(coin.pos.x, coin.pos.y)

```

Then I don’t move at all.

---

<div class="post-metadata">

**Author:** ![RangerGrant9307](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/rangergrant9307/32/26001_2.png) [@RangerGrant9307](https://discourse.codecombat.com/u/RangerGrant9307)\
**Post date:** [August 10, 2021, 11:08pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/14 "2021-08-10T23:08:15Z")

</div>

Nooooo! nearest.pos.x, nearest.pos.y!!!

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [August 10, 2021, 11:10pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/15 "2021-08-10T23:10:55Z")

</div>

I am doing exactly what you are telling me, and I don’t move.

```python
# This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.
hero.findItems()
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:
            nearest = coin
            nearestDistance = coin.distance
            
            hero.moveXY(nearest.pos.x, nearest.pos.y)

            # Set nearest to coin
            
            # Set nearestDistance to 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.

```

---

<div class="post-metadata">

**Author:** ![RangerGrant9307](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/rangergrant9307/32/26001_2.png) [@RangerGrant9307](https://discourse.codecombat.com/u/RangerGrant9307)\
**Post date:** [August 10, 2021, 11:11pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/16 "2021-08-10T23:11:54Z")

</div>

Okay, lemme use ur code, and see wut happens

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [August 10, 2021, 11:14pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/17 "2021-08-10T23:14:28Z")

</div>

That is my code, and I don’t move.

---

<div class="post-metadata">

**Author:** ![RangerGrant9307](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/rangergrant9307/32/26001_2.png) [@RangerGrant9307](https://discourse.codecombat.com/u/RangerGrant9307)\
**Post date:** [August 10, 2021, 11:14pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/18 "2021-08-10T23:14:34Z")

</div>

> [@enPointe77](#):
>
> ```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:
> nearest = coin
> nearestDistance = coin.distance
>             
> hero.moveXY(nearest.pos.x, nearest.pos.y)
> 
> # Set nearest to coin
> 
> ```

Try this.

```python

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

```

---

<div class="post-metadata">

**Author:** ![RangerGrant9307](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/rangergrant9307/32/26001_2.png) [@RangerGrant9307](https://discourse.codecombat.com/u/RangerGrant9307)\
**Post date:** [August 10, 2021, 11:15pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/19 "2021-08-10T23:15:07Z")

</div>

Oops, formatted it now/

---

<div class="post-metadata">

**Author:** ![RangerGrant9307](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/rangergrant9307/32/26001_2.png) [@RangerGrant9307](https://discourse.codecombat.com/u/RangerGrant9307)\
**Post date:** [August 10, 2021, 11:17pm UTC](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795/20 "2021-08-10T23:17:09Z")

</div>

Its bugged. Look at the interpreter/  
|Perkele’s Archer| Hero Placeholder had new Programmable problem: plan Line 15: TypeError: Tried to load an unsafe native value into the interperter:function / function(thang, fromEdges) {  
var _ref;  
if (fromEdges == null) {  
fromEdges = false;  
}  
if (!(thang && (thang.isVector || thang.isThang || (!_.isNaN(thang.x + thang.y))))) {  
console.log(‘distance from’, this.id, ‘to’, thang != null ? thang.id : void 0, ‘did not work: isVector’, thang != null ? thang.isVector : void 0, ‘isThang’, thang != null ? thang.isThang : void 0, ‘x’, thang != null ? thang.x : void 0, ‘y’, thang != null ? thang.y : void 0, ‘keys’, \_.keys(thang));  
throw new ArgumentError((\_ref = this.contextPhysical) != null ? \_ref.error1 : void 0, “distance”, “target”, “object”, thang);  
}  
return Math.sqrt(this.distanceSquared(thang, fromEdges));  
} 0

[Next page](https://discourse.codecombat.com/t/solved-sand-snakes-python/29795.md?page=2)
