Hi there, I’m having troubles with this level : sand snakes

here is my code:

# 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
# Cherche parmi toutes les pièces celle qui est le plus proche.
while coinIndex < len(coins):
coin = coins[coinIndex]
coinIndex += 1
distance = hero.distanceTo(coin)
# Si la distance de la pièce est moins que la nearestDistance
if distance < nearestDistance:
# Set nearest to coin
nearest = coin
# Set nearestDistance to distance
nearestDistance = distance
if nearest:
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.

So my hero runs to the first coin, then it goes straight to the mine cause I think he wants to take the coin which is behind the mines.
I don’t understand what’s wrong with my code
could you help please ?

while True:
coins = hero.findItems()
coinIndex = 0
nearest = None
nearestDistance = 9999
# Cherche parmi toutes les pièces celle qui est le plus proche.
while coinIndex < len(coins):
coin = coins[coinIndex]
coinIndex += 1
distance = hero.distanceTo(coin)
# Si la distance de la pièce est moins que la nearestDistance
if distance < nearestDistance:
# Set nearest to coin
nearest = coin
# Set nearestDistance to distance
nearestDistance = distance
if nearest:
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.

I realized that it wasnt out of the while loop, but I still don’t understand why it’s not inside the “if distance < nearestDistance …”

This level requires you to find the nearest coin and move to it. So the process you did, is what made you get the nearest coin.

So the bunch of variables you have set above will be used as you can see later on in the while loop.

This while-loop over here, it loops over all the coins you can see.
It goes this way, as long as coinIndex (which is a cursor or pointer here) is less than the length of the array of available coins, the coin is the coinIndex of coins, coinIndex is a number, and when you have an array, you have slots, so each slot is called an index and has a number, starting from 0, so the first index would be 0. After you defined coin, you defined the distance.

The if statement checks if the distance of the coin you currently are pointing at is less than the closest distance you have, if it does, it makes sure the nearest distance is reset to the really closest one, and sets the nearest coin to the really closest coin.

So for your question on why we put it outside the second while loop. After all the coins have been iterated or looped through, you have the variables all right set, so when you want to go to the nearest coin, you go to the really nearest coin. If we put it inside the if loop, the nearest coin might be another one, not the one in the variable, because not all the coins would have been checked, once they all are, the inner while loop breaks