Bookkeeper problem with coin count(python)


#1

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)


#2

Please format you code according to the FAQ.


#3

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


#4

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


#5

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


#6

IC,thx so much…


#7

Yeah! I had the same problem.


#8

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:
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 :wink: