[SOLVED] Sand Snakes PLEASE HELP

Hey, I really need help here. I’ve been trying for an hour, looking through other help sites. Here is my 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 coin.distance < nearestDistance:
            # Set nearest to coin
            coin = nearest
            # Set nearestDistance to distance
            distance = nearestDistance
            
    # 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)

I am standing doing nothing. Please help.

With your code, since coinIndex is always 0, coin is always coins[0] and your while coin index < len(coins) loop never ends.

try getting rid of the nearest and put it under the

        if coin.distance < nearestDistance:
            # Set nearest to coin
            coin = nearest
            # Set nearestDistance to distance
            distance = nearestDistance

and also

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!

I will try it, but I think that is how it already is.

Welcome welcome @Griffi22 to the forum! It is a lovely place where you can share all kinds of stuff (appropriate of course), share bugs, and even get assistance for code! We suggest that you review this topic which shows all essentials of this board! And we suggest you review this topic which shows how to post your code correctly! Thanks!! :partying_face: :partying_face:

Can you send me the link to this level?

Lydia

https://codecombat.com/play/level/sand-snakes?
Why do you need it?

Also, if someone could just post the correct code, that would be great.

that is kinda against the rules

and also to help you @Griffi22

Ok. This is my first time posting, but I’ve read through other posts, so I got the gist of what to do.

well just trying to be helpful to people

Also here is my screen. I have the Rune-Sword, obsidian shield, and obsidian helmet for gear that is not required.

So I can help you on this level, and please do not request solutions as it is against the Discourse rules.
Lydia

This is supposed to be

nearest = coin
            # Set nearestDistance to distance
            nearestDistance = distance

You just did it the other way around.
Lydia

Sorry, what is the difference between those codes? Did you post the same thing twice? I see: coin = nearest and distance = nearestDistance two times each?? Did I indent wrong

1 Like

Oops! Sorry, I fixed it now.
Lydia

I did what you said @Lydia_Song, but it did not work, though instead of standing there over an X, I am now just standing there.

Is my hero just lazy? :rofl: But really, what’s wrong?

2 Likes

Can you post your newest code?
Lydia