Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Sand Snakes (Python) These levels are throwing me for a loop[SOLVED]

Hey guys,

I’m getting really stuck with these loops. My character just keeps walking straight in to a mine. I have changed the indentation a few times of the MoveXY but it just doesnt work.

Here is my code. Many thanks.

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
            nearestDistace = 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(coin.pos.x, coin.pos.y)

Did you mean to make it that high?

thats pre-loaded code. its supposed to be like that.

Okay, thanks. I can’t find an error, sorry. :cry: But maybe @abc, @PeterPalov, @Lydia_Song or/and @milton.jinich could help :slight_smile:

1 Like

delete this.
20 characters

still wont work.
he walks in to a mine (a different one but still).
her is the new code.

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
            nearestDistace = 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(coin.pos.x, coin.pos.y)

Change the coin to nearest

changing it to nearest still doesnt work :frowning:

Post your code
20 char

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

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
            nearestDistace = 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)

Are you using the Mimic pet?

no. i have no pet and i have also removed alot of my armour etc.
here is a screenshot.

I’m actually stumped. I looked at your code and it’s the EXACT SAME code as mine. @Chaboi_3000 is this a bug?

1 Like

weird. i’m going to try it on a different device in the meantime.

Thanks for looking in to this for me.

try resubmitting it seems like a bad seed to me

resubmitted 3 times. same result :frowning:

hmm can you show me with armour?

He/she already did.
20 ch

i dont see any screen shot with armour

just put this on with the same result.