Sand Snakes - Python

my
hero
never
moves
and
the
clues
are
useless
please
help

# 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.
coins = 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:
            # 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.
    


https://codecombat.com/play/level/sand-snakes?

After the if loop, there should be another if loop checking for nearest. Then you need to move to nearest’s position with moveXY.

My hero still doesnt move

Move with hero.moveXY to the nearest’s position x and position y.

That is exactly what i did.

# 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)

waiit… (20 chars)

nope.

    if nearest:
        hero.moveXY(coin.pos.x, coin.pos.y)

dont work.

Coin should be nearest. Maybe delete the if statement.

You literally told me to make it in the first place.

iT SHOULD BE
(Oops caps lock)

if (nearest) {
hero.moveXY(nearest.pos.x, nearest.pos.y);

HE/SHE IS USING PYTHON (oops caps lock)

1 Like

it seems that youve already defined

so remove the coin.All you need now is to check if there is a nearest coin and move to it :wink:

was the good answer but to make sure you did it right can you please post your code?

2 Likes

Ok now its being dumb and saying that my nearest code in the first .pos should be a hero.pos and giving me an error.

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

Ohp nevermind i put it in a if statement and the error went away

But my hero STILL DOESNT MOVE

You’ve already defined coin distance, you don’t need to do this. You can substite it with

Lydia

There is no coin.distance in my code. I have no idea what you are looking at.

# 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.
coins = 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(nearest)
        # 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)

No, there is.

Where? I have looked through my code THREE TIMES. I swear its not there.