Sand snakes/ Python/ Stuck

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

# 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

2 Likes

Can you give me a link to the level?

change the if to while

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

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

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

This is my if statement:

if coin.distance < nearestDistance:

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

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

Sure
CodeCombat - Coding games to learn Python and JavaScript?

Like this?

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

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

Yea, the default code was while and not if

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

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)

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

maybe put this at the end

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

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

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

like this?

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

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

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