what am I doing wrong?

``````while True:
closestGold = None
minGoldDist = 9001
coinIndex = 0
coins = hero.findItems()
coin=coins[coinIndex]
# Найди ближайшую золотую монету.
# Запомни: золотая монета имеет ценность 3.
while len(coins)>coinIndex:
distance = hero.distanceTo(coin)
if coin.value==3 and distance<minGoldDist:
closestGold=coin
if closestGold:
#А теперь доберись до ближайшей золотой монеты и возьми её!
hero.moveXY(closestGold.pos.x, closestGold.pos.y)
coinIndex+=1
pass
``````

Put this in the while loop

problem solved by half. I have a hero running on gold for height. first 2 then 3 then 5. not the nearest one runs. How to make it run on a close gem?

You have to check the distance of the coin after checking that the value is 3.

I still haven’t solved it. do not understand what and how should check you write.

``````while True:
coin=coins[coinIndex] # you don't need this line
# in your case coin = coins[0]
# Найди ближайшую золотую монету.
# Запомни: золотая монета имеет ценность 3.
while len(coins) > coinIndex:
# put coin = coins[coinIndex]  # your coin variable will be undefined
# if you don't put this line
distance = hero.distanceTo(coin)
if coin.value==3 and distance < minGoldDist:
closestGold=coin
# minGoldDist = distance  # change the min to gold distance
# with the current distance
# block of code that have to be
# outside the   while len(coins) > coinIndex loop

coinIndex +=1
if closestGold:
#А теперь доберись до ближайшей золотой монеты и возьми её!
hero.moveXY(closestGold.pos.x, closestGold.pos.y)
pass
``````
1 Like