# Bookkeeper problem with coin count(python)

**URL:** <https://discourse.codecombat.com/t/bookkeeper-problem-with-coin-count-python/10766>\
**Category:** Uncategorized\
**Created:** [March 1, 2017, 5:22pm UTC](https://discourse.codecombat.com/t/bookkeeper-problem-with-coin-count-python/10766 "2017-03-01T17:22:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sui\_Shing\_Lung](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sui_shing_lung/32/5646_2.png) [@Sui\_Shing\_Lung](https://discourse.codecombat.com/u/Sui_Shing_Lung)\
**Post date:** [March 1, 2017, 5:22pm UTC](https://discourse.codecombat.com/t/bookkeeper-problem-with-coin-count-python/10766/1 "2017-03-01T17:22:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Luke10](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/luke10/32/19892_2.png) [@Luke10](https://discourse.codecombat.com/u/Luke10)\
**Post date:** [March 1, 2017, 6:11pm UTC](https://discourse.codecombat.com/t/bookkeeper-problem-with-coin-count-python/10766/2 "2017-03-01T18:11:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)\
**Post date:** [March 2, 2017, 5:48am UTC](https://discourse.codecombat.com/t/bookkeeper-problem-with-coin-count-python/10766/3 "2017-03-02T05:48:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sui\_Shing\_Lung](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sui_shing_lung/32/5646_2.png) [@Sui\_Shing\_Lung](https://discourse.codecombat.com/u/Sui_Shing_Lung)\
**Post date:** [March 2, 2017, 12:21pm UTC](https://discourse.codecombat.com/t/bookkeeper-problem-with-coin-count-python/10766/4 "2017-03-02T12:21:43Z")

</div>

thx… I solved it ,But y i can’t count it as Hoarding Gold level does?

---

<div class="post-metadata">

**Author:** ![Bryukh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bryukh/32/19597_2.png) [@Bryukh](https://discourse.codecombat.com/u/Bryukh)\
**Post date:** [March 2, 2017, 12:23pm UTC](https://discourse.codecombat.com/t/bookkeeper-problem-with-coin-count-python/10766/5 "2017-03-02T12:23:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sui\_Shing\_Lung](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sui_shing_lung/32/5646_2.png) [@Sui\_Shing\_Lung](https://discourse.codecombat.com/u/Sui_Shing_Lung)\
**Post date:** [March 6, 2017, 7:50am UTC](https://discourse.codecombat.com/t/bookkeeper-problem-with-coin-count-python/10766/6 "2017-03-06T07:50:01Z")

</div>

IC,thx so much…

---

<div class="post-metadata">

**Author:** ![zane19191](https://avatars.discourse-cdn.com/v4/letter/z/b782af/32.png) [@zane19191](https://discourse.codecombat.com/u/zane19191)\
**Post date:** [March 21, 2017, 3:18pm UTC](https://discourse.codecombat.com/t/bookkeeper-problem-with-coin-count-python/10766/7 "2017-03-21T15:18:30Z")

</div>

Yeah! I had the same problem.

---

<div class="post-metadata">

**Author:** ![bennypr0fane](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/bennypr0fane/32/8337_2.png) [@bennypr0fane](https://discourse.codecombat.com/u/bennypr0fane)\
**Post date:** [April 2, 2018, 9:01pm UTC](https://discourse.codecombat.com/t/bookkeeper-problem-with-coin-count-python/10766/8 "2018-04-02T21:01:12Z")

</div>

> [@Bryukh](#):
>
> Use hero.gold to watch for you gold instead count it.

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 `+=` :

```python
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`:  
 ![totalGold](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/f/f228b175b6b7afb2a4f9d5130226a9e8e0bc2295.png)

And in “Bookkeeper”, the counting method is used again for number of defeated enemys:

```python
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:

> [@Bryukh](#):
>
> 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

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 😉
