# [Solved] Sand snakes (Python)

Hi, I am struggling on how to get started on this level. I have no idea what to do. This is the given code:

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

# Set nearest to coin

# Set nearestDistance to 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.

``````

Can someone give me ideas on how to begin?

OK heres the gist.
Loop thru all the coins.
If the coins distance to you is smaller then nearest distance.
Make nearestDistance to the distance, and make nearest to the coin.
So lets say you start.
Coin A is close then 9999.
So you pick it up.
Then,since COIN A is gone, you get coinB.
If its closer then 9999:
Pick it up.
Its kinda like finding the weakest enemy.
You make weakest to none, and make weakest health to 9999
Then, loop thru all enemies, and if its health is less then weakest, make it weakest.
Lets say, there is an ogre, a shaman, and a thrower.
120 is less then 9999 so weakestHealth is 120, and ogre is weakest. Then, you find the shaman in the loop.
60 is less then 120, so weakestHealth is 60, and the shaman is weakest.
Then, you find the thrower.
7 is less then 60, so then weakestHealth is 7, and thrower is weakest.
If the game spawns an enemy thatās health is less then 7, its weakest enemy.
Here is some code for getting the weakest enemy, and it might give you some ideas for something similar for the NEAREST item.

``````while True:
weakest = None
leastHealth = 99999
enemies = hero.findEnemies()
for enemy in enemies:
if enemy.health < leastHealth:
weakest = enemy
lowestHealth = enemy.health
# Loop through all enemies.

if weakest:
hero.attack(weakest)
``````

Then, for the items, just do it with distance, and do items instead of enemies.
I hope this helps.

This is pretty clear, just follow the instructions

Thanks, I will try this tomorrow.
@Lydia_Song, those instructions made me look like this ā. They didnāt help me.

the instructions are basically telling you write code that finds out if the coin is the closest.

like imagine coinA, coinB, and coinC
coinA is 5 m away, coinB is 3 m away, and coinC is 13 m away

you have to cycle through every coin and see which is the closest.
nearestDistance = 999
nearest = 0
coinA distance < nearestDistance so coinA distance becomes the new nearest distance and coinA becomes nearest

nearestDistance = 5

coinB distance < nearestDistance so coinB becomes nearest and repeat.

So basically, youāre looping through all the coins to find the nearest coin.
They gave you the nearest as None and nearestDistance at 9999 (cause its just a starting distance and every coinās distance would be lower than that)

So here itās telling you, if the distance to the coin is less than the nearestDistance, do `nearest = coin.` And do `nearestDistance = distance`.
If there is a nearest, use moxeXY to move to the nearest coin

Iām still struggling on this code. I think Iāve got most of it, but there are two spots I canāt figure it out. Hereās my current code:

``````# 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 item.distance < nearestDistance:
nearest = item
nearestDistance = item.distance
# Set nearest to coin

# Set nearestDistance to 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(item.pos.x, item.pos.y)

``````

I donāt get what to do right before the given code, when it says ` You'll need to find the nearest coins on your own.` And I also donāt understand what to do when it says `# If this coin's distance is less than the nearestDistance `

Also, in this entire spot:

`````` if item.distance < nearestDistance:
nearest = item
nearestDistance = item.distance
``````

Should I write coin instead of item?

I switched it out with coins, and now my hero moves. She picks up 1 coin, and blows up.

`````` This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.
hero.findItems()
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:
nearest = coin
nearestDistance = coin.distance
# Set nearest to coin

# Set nearestDistance to 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)

``````

Just put your move command into the while loop. LOL

Like that?

``````  while True:
hero.moveXY(coin.pos.x, coin.pos.y)

``````

My hero still blows up. So Iām not sure I fully understand what youāre trying to say.

``````        if coin.distance < nearestDistance:
nearest = coin
nearestDistance = coin.distance
moooooov
``````
``````        if coin.distance < nearestDistance:
nearest = coin
nearestDistance = coin.distance

hero.moveXY(coin.pos.x, coin.pos.y)

``````

Then I donāt move at all.

Nooooo! nearest.pos.x, nearest.pos.y!!!

1 Like

I am doing exactly what you are telling me, and I donāt move.

``````# This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.
hero.findItems()
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:
nearest = coin
nearestDistance = coin.distance

hero.moveXY(nearest.pos.x, nearest.pos.y)

# Set nearest to coin

# Set nearestDistance to 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.

``````

Okay, lemme use ur code, and see wut happens

That is my code, and I donāt move.

Try this.

``````
while True:
coins = hero.findItems()
coinIndex = 0
nearest = None
nearestDistance = 9999
while coinIndex < len(coins):
coin = coins[coinIndex]
coinIndex += 1
distance = hero.distanceTo(coin)
if distance < nearestDistance:
nearest = coin
nearestDistance = coin.distance
hero.moveXY(nearest.pos.x, nearest.pos.y)
``````

Oops, formatted it now/

Its bugged. Look at the interpreter/
|Perkeleās Archer| Hero Placeholder had new Programmable problem: plan Line 15: TypeError: Tried to load an unsafe native value into the interperter:function / function(thang, fromEdges) {
var ref;
if (fromEdges == null) {
fromEdges = false;
}
if (!(thang && (thang.isVector || thang.isThang || (!
.isNaN(thang.x + thang.y))))) {
console.log(ādistance fromā, this.id, ātoā, thang != null ? thang.id : void 0, ādid not work: isVectorā, thang != null ? thang.isVector : void 0, āisThangā, thang != null ? thang.isThang : void 0, āxā, thang != null ? thang.x : void 0, āyā, thang != null ? thang.y : void 0, ākeysā, _.keys(thang));
throw new ArgumentError((_ref = this.contextPhysical) != null ? _ref.error1 : void 0, ādistanceā, ātargetā, āobjectā, thang);
}
return Math.sqrt(this.distanceSquared(thang, fromEdges));
} 0