# Sand snakes/ Python/ Stuck

**URL:** <https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621>\
**Category:** Level Help\
**Created:** [February 25, 2021, 2:22pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621 "2021-02-25T14:22:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Some\_Guy](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/some_guy/32/20317_2.png) [@Some\_Guy](https://discourse.codecombat.com/u/Some_Guy)\
**Post date:** [February 25, 2021, 2:22pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/1 "2021-02-25T14:22:06Z")

</div>

My character keeps moving to the first coin then walking into the mines and im not sure whats wrong

```python
# 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.
    if coinIndex < len(coins):
        coin = coins[coinIndex]
        coinIndex += 1
        distance = hero.distanceTo(coin)
        # If this coin's distance is less than the nearestDistance
        if 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 coin:
                hero.moveXY(nearest.pos.x , nearest.pos.y)

```

also i am aware that all i need is a flag and i can win but i would prefer to learn the full lesson

---

<div class="post-metadata">

**Author:** ![JoPro\_8000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jopro_8000/32/27121_2.png) [@JoPro\_8000](https://discourse.codecombat.com/u/JoPro_8000)\
**Post date:** [February 25, 2021, 2:35pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/2 "2021-02-25T14:35:51Z")

</div>

Can you give me a link to the 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:** [February 25, 2021, 2:36pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/3 "2021-02-25T14:36:59Z")

</div>

> [@Some\_Guy](#):
>
> `if coinIndex < len(coins):`

change the if to while

---

<div class="post-metadata">

**Author:** ![JoPro\_8000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jopro_8000/32/27121_2.png) [@JoPro\_8000](https://discourse.codecombat.com/u/JoPro_8000)\
**Post date:** [February 25, 2021, 2:38pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/4 "2021-02-25T14:38:52Z")

</div>

Okay, so I could not figure out the code when I was completing the level myself, so I just used flags. You should not do that. That’s just abusing the level. Check if all of your code is indented properly

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [February 25, 2021, 2:40pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/5 "2021-02-25T14:40:31Z")

</div>

I found other hacks(won’t tell you) but it was actually more complex than the level itself.

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [February 25, 2021, 2:41pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/6 "2021-02-25T14:41:41Z")

</div>

I also made it in 3rd place in making through the fastest.

---

<div class="post-metadata">

**Author:** ![JoPro\_8000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jopro_8000/32/27121_2.png) [@JoPro\_8000](https://discourse.codecombat.com/u/JoPro_8000)\
**Post date:** [February 25, 2021, 2:42pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/7 "2021-02-25T14:42:19Z")

</div>

This is my if statement:

```python
if coin.distance < nearestDistance:

```

Idk if it is the correct one but maybe change it to this?

---

<div class="post-metadata">

**Author:** ![JoPro\_8000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jopro_8000/32/27121_2.png) [@JoPro\_8000](https://discourse.codecombat.com/u/JoPro_8000)\
**Post date:** [February 25, 2021, 2:43pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/8 "2021-02-25T14:43:03Z")

</div>

Nvm. I think the only error is the one that @Eric_Tang mentioned

---

<div class="post-metadata">

**Author:** ![Some\_Guy](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/some_guy/32/20317_2.png) [@Some\_Guy](https://discourse.codecombat.com/u/Some_Guy)\
**Post date:** [February 25, 2021, 2:45pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/9 "2021-02-25T14:45:12Z")

</div>

Sure  
[CodeCombat - Coding games to learn Python and JavaScript](https://codecombat.com/play/level/sand-snakes)?

---

<div class="post-metadata">

**Author:** ![Some\_Guy](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/some_guy/32/20317_2.png) [@Some\_Guy](https://discourse.codecombat.com/u/Some_Guy)\
**Post date:** [February 25, 2021, 2:47pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/10 "2021-02-25T14:47:48Z")

</div>

Like this?

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

```

because it is still doing the same thing

---

<div class="post-metadata">

**Author:** ![JoPro\_8000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jopro_8000/32/27121_2.png) [@JoPro\_8000](https://discourse.codecombat.com/u/JoPro_8000)\
**Post date:** [February 25, 2021, 2:47pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/11 "2021-02-25T14:47:48Z")

</div>

So, I would advise checking over your code to see if everything is indented. I have not figured to answer out yet, because I used flags earlier. @Chaboi_3000 just saying, you may want to restrict flags in this level

---

<div class="post-metadata">

**Author:** ![JoPro\_8000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jopro_8000/32/27121_2.png) [@JoPro\_8000](https://discourse.codecombat.com/u/JoPro_8000)\
**Post date:** [February 25, 2021, 2:48pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/12 "2021-02-25T14:48:08Z")

</div>

Yea, the default code was while and not if

---

<div class="post-metadata">

**Author:** ![Some\_Guy](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/some_guy/32/20317_2.png) [@Some\_Guy](https://discourse.codecombat.com/u/Some_Guy)\
**Post date:** [February 25, 2021, 2:49pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/13 "2021-02-25T14:49:37Z")

</div>

I changed it back to while but it still isn’t working did I do it right

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

```

---

<div class="post-metadata">

**Author:** ![JoPro\_8000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jopro_8000/32/27121_2.png) [@JoPro\_8000](https://discourse.codecombat.com/u/JoPro_8000)\
**Post date:** [February 25, 2021, 2:51pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/14 "2021-02-25T14:51:24Z")

</div>

Idk, im helping someone else right now, could you ask @Eric_Tang or @milton.jinich to help you?

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [February 25, 2021, 2:51pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/15 "2021-02-25T14:51:29Z")

</div>

maybe put this at the end

> [@Some\_Guy](#):
>
> `coinIndex += 1`

---

<div class="post-metadata">

**Author:** ![Some\_Guy](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/some_guy/32/20317_2.png) [@Some\_Guy](https://discourse.codecombat.com/u/Some_Guy)\
**Post date:** [February 25, 2021, 2:54pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/16 "2021-02-25T14:54:08Z")

</div>

Like this? Because now my character is just moving a bit then stopping infinity not collecting a single coin

```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]
        
        distance = hero.distanceTo(coin)
        # If this coin's distance is less than the nearestDistance
        if 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 coin:
                hero.moveXY(nearest.pos.x , nearest.pos.x) 
                coinIndex += 1

```

@milton.jinich

---

<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:** [February 25, 2021, 3:02pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/17 "2021-02-25T15:02:30Z")

</div>

> [@Some\_Guy](#):
>
> ```python
> coin = coins[coinIndex]
>         
> distance = hero.distanceTo(coin)
> 
> ```

your missing a line in between coins[coinIndex] and distance = hero.distanceTo(coin) add  
coinIndex += 1

---

<div class="post-metadata">

**Author:** ![Some\_Guy](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/some_guy/32/20317_2.png) [@Some\_Guy](https://discourse.codecombat.com/u/Some_Guy)\
**Post date:** [February 25, 2021, 3:09pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/18 "2021-02-25T15:09:02Z")

</div>

like this?

```python
while coinIndex < len(coins):
        coin = coins[coinIndex]
        distance = hero.distanceTo(coin)
        coinIndex += 1

```

---

<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:** [February 25, 2021, 3:10pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/19 "2021-02-25T15:10:12Z")

</div>

mmhhmm correct just add that one line and your good to go.

---

<div class="post-metadata">

**Author:** ![Some\_Guy](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/some_guy/32/20317_2.png) [@Some\_Guy](https://discourse.codecombat.com/u/Some_Guy)\
**Post date:** [February 25, 2021, 3:11pm UTC](https://discourse.codecombat.com/t/sand-snakes-python-stuck/27621/20 "2021-02-25T15:11:13Z")

</div>

What do you mean by line?  
Im sorry if im just missing something obvious

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