# I have problem counting the number of coins the hero collected. The hero collected 16 coins but he said he collected 13. What is the problems of my code? Hero is my code in coin counting

collected = 0
while True:
coin = hero.findNearestItem
if coin:
hero.moveXY(coin.pos.x, coin.pos.y)
collected += coin.value
if hero.time > 30
break

hero.move(59, 33)
hero.say(collected)

1 Like

Please format you code according to the FAQ.
https://discourse.codecombat.com/faq

1 Like

Use `hero.gold` to watch for you gold instead count it.

2 Likes

thxâ€¦ I solved it ,But y i canâ€™t count it as Hoarding Gold level does?

2 Likes

Looks careful for your hero â€“ sometimes you collect coins while your hero is moving for another one. As the result you donâ€™t count coins which you take occasionally

2 Likes

IC,thx so muchâ€¦

2 Likes

Yeah! I had the same problem.

2 Likes

Sorry for resurrecting and old thread, but I think I may have something to add here:
In â€śHoarding Goldâ€ť (the level before Bookkeeper), gold is counted using a variable `totalGold` and `+=` :

``````totalGold = 0
while True:
coin = hero.findNearestItem()
if coin:
# Sammle die MĂĽnzen auf.
hero.moveXY(coin.pos.x, coin.pos.y)
# Addiere den MĂĽnzwert (value) zur Goldsumme (totalGold) hinzu. (fĂĽr mehr Details siehe Anleitung)
totalGold += coin.value
# Erhalte seinen Wert mit:  coin.value
pass
if totalGold >= 25:
# Dies bricht aus der Schleife aus und fĂ¤hrt mit dem Code darunter fort.
# Die Schleife ist beendet, jetzt wird der Code nach der Schleife ausgefĂĽhrt.
break
``````

Also, the hints for this level suggest to report the final amount of gold using the variable `totalGold`:

And in â€śBookkeeperâ€ť, the counting method is used again for number of defeated enemys:

``````defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
``````

I believe it would come natural to most people that they should use the same technique for counting the coins - and reporting it to Naria! That also resulted for me in this perceived bug of hero reporting the counted gold instead of current amount => wrong, because he had collected an extra coin in passing, like you said:

The `hero.gold` function isnâ€™t explicitly mentioned/explained, so maybe it would be a good idea to say it in the hints that counting isnâ€™t necessary in this level and people wonâ€™t run into this problem anymore - or more postings for this topic

2 Likes