# Cloudrip.Mountain_Ogre.Gorge.Gougar

Hello guys!,

This is my first post in the platform, sorry if it is not the correct place for my question.

Here you can see my code about how to obtain 60 coins before the decision to move and build the fence.

The problem here is the “counter”, for some reason it doesn’t match with the real one that you can see up and right in the corner. The hero (function) is not considering the real value of each different coin. I can confirm it using the function hero.say(coinCount) after each coin taken.

I have made a lot of changes and I dont know how to fix it and with this problem the hero will never “understand” he has already have 60 coins.

``````coinCount = 0

while True:
closestCoin = None
minDistance = float('inf')
coins = hero.findItems()

for coin in coins:
distance = hero.distanceTo(coin) / coin.value
if distance < minDistance:
closestCoin = coin
minDistance = distance

if closestCoin:
hero.moveXY(closestCoin.pos.x, closestCoin.pos.y)
coinCount += closestCoin.value
hero.say(coinCount)

if coinCount >= 60:
break
``````

You don’t need to add coin values manually; `hero.gold` will find it automatically. (which is the value in the top right) Although you will need at least the Quartz Sense Stone to be able to use `hero.gold`.

1 Like

Thanks a lot!

I have successfully completed the code for this level.

``````while hero.gold <= 60:
closestCoin = None
minDistance = float('inf')
coins = hero.findItems()

for coin in coins:
distance = hero.distanceTo(coin)
if distance < minDistance:
closestCoin = coin
minDistance = distance

if closestCoin:
if minDistance > 5 and hero.isReady("jump"):
hero.jumpTo(closestCoin.pos)
else:
hero.moveXY(closestCoin.pos.x, closestCoin.pos.y)

hero.moveXY(16, 38)
hero.buildXY("fence", 21, 38)
hero.cast("chain-lightning", hero.findNearestEnemy())

``````

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.