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

# [SOLVED] Sand Snakes (ran out of time)

**CreeperFoot**#1

**Gamestack**#2

@CreeperFoot If nearest has to be in the while true loop, not out of the while true loop

**Ashirg9**#4

He is right,

has to be IN the loop. Like:

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

Greets

**Ashirg9**#7

Nice to hear.

You can ask everytime if you have another problem, the community is ready to help