Sand snakes/ Python/ Stuck

put every thing in a while-true-loop

It is i just forgot to copy it let me fix it

Uh did that fix the problem

No it already was all in a while-loop i just didn’t paste right. Its still doing the same thing

Delete this and move t

this thing and make it so it alines with this while coinIndex < len(coins):

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

Almost, almost move this hero.moveXY(nearest.pos.x , nearest.pos.x) back so it alines with if distance < nearestDistance:

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

Use a pair of boots that only have moveXY. Also, you marked a post as solved, have you completed the level yet?

No i miss clicked and ok i will try a different boot pair

If you misclicked, un-click the solution button. You don’t want this topic accidently closed.

sorry I made a mistake make it so this hero.moveXY(nearest.pos.x , nearest.pos.x) alines with while True:

Which comment is it?

Like this:

While True:
    #rest of your code
    # if statement checking for nearest
        #moveXY to nearest

The if statement and the moveXY statement should be outside of the while loop, and aligned with the while true loop.

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

Yes, but check if there is nearest.

if nearest:
    #move

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

Change the coin to nearest.

Its still not working

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)

Your code is right in what is says, but this bit has the wrong indentation (how much whitespace there is behind it):

You should only run those lines after the while loop has finished running. That means it should be after it, and at the same level in the while True loop as it is. At the moment the if nearest: is inside the while loop, which means it will run for every coin. This won’t work because you hero will move to a coin before all of the coins have been checked. You might have left the closest one out.
Danny

1 Like