 # Sand Snakes [SOLVED]

#1

Hello, I have been stuck on this level for a while and finally decided to ask for help. I don’t understand what i’m doing wrong.

Here’s 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 self.distanceTo(coin) < nearestDistance:
# Set nearest to coin
item = hero.findNearestItem()
# 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.
self.moveXY(nearest.pos.x, nearest.pos.y)

``````
#2
#3

#1. You are making a variable distance, but you are not using it.
#2 You are using .self method but you use hero method before.
#3 You are telling your hero to move to Nearest and Nearest value will never be something other than “None” because you did not define it in your while loop.

#4 You are using extra line of code that can be removed there, as coins is already defined at beginning

1 Like
#4

Most of your code is fine, just a few mistakes. Firstly, after the comment, `set nearest to coin`, you’ve said, `item = hero.findNearestItem`. The point of this level is that findNearestItem doesn’t work. You are looping over an array and comparing your distance to each coin in that array, until you find the one closest to you. You have also said `item` when the code above defines it as `coin`. What you need to put is `nearest = coin` thereby setting the current coin as the `nearest` coin. Secondly, after the comment `if this coin's distance is less than the nearestDistance` you have restated `if self.distanceTo(coin)`. You have already defined distance in the line above, so to keep you code neat, you might as well just say `if distance`. Finally, you’ve put `self.moveXY(nearest.pos.x, nearest.pos.y)` in the while True loop. This will not work. You need to put the move command outside the `while coinIndex < len(coins)` loop, so that the loop can find the nearest coin. Then you move to it. I hope this helps!

3 Likes
#5

I fixed my code but now he just runs in place.

Here’s 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 self.distanceTo(coin) < 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.
while coinIndex < len(coins):
hero.moveXY(nearest.pos.x, nearest.pos.y)
``````

#6

To fix that, remove your `while coinIndex < len(coins)` loop at the bottom, and instead put outside the first `while coinIndex < len(coins)`:

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

``````

This will allow your loop to identifiy the nearest coin, and then after it has found the nearest one, you move to it.

1 Like
#7

Okay, I fixed it and my hero is finally moving but he is not defining the coins he is defining an item. I’m not sure what i’m not doing right.

Here’s 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 self.distanceTo(coin) < 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)
``````

#8

You need to put your

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

out side of the `while coinIndex < len(coins):` loop

2 Likes
#9

ohhh okay, sorry I didn’t understand. I passed the level! Thanks for helping me out! #10

Glad I could help #11

I put solved
(20 chars)

#12

how can you do that?

#13

You have to be trust level three.

#14

Thanks, couldn’t work out how to do that.