[Solved] Hoarding Gold

# Collect 25 gold, and then tell Naria the total.
# Use break to stop collecting when totalGold >= 25.

totalGold = 0
while True:
    coin = hero.findNearestItem()
    if coin:
        # Pick up the coin.
        hero.move(coin.pos)
        # Add the coin's value to totalGold.
        # Get its value with:  coin.value
        totalGold = totalGold + coin.value
        pass
    if totalGold >= 25:
        # This breaks out of the loop to run code at the bottom.
        # The loop ends, code after the loop will run.
        break

# Done collecting gold!
hero.moveXY(58, 33)
# Go to Naria and say how much gold you collected.
hero.say(totalGold)

  1. If I do this, my hero takes the first gold coin (3 values), and then tries to get to the next coins but, like 0.1 sec later, she turns her way straight to Naria and says ā€œ25ā€ when the money I got is still only just 3.

  2. and if I do this belowā€¦
    (only changed ā€œtotalGold = totalGold + coin.valueā€ to ā€œtotalGold = hero.gold + coin.valueā€)

# Collect 25 gold, and then tell Naria the total.
# Use break to stop collecting when totalGold >= 25.

totalGold = 0
while True:
    coin = hero.findNearestItem()
    if coin:
        # Pick up the coin.
        hero.move(coin.pos)
        # Add the coin's value to totalGold.
        # Get its value with:  coin.value
        totalGold = hero.gold + coin.value
        pass
    if totalGold >= 25:
        # This breaks out of the loop to run code at the bottom.
        # The loop ends, code after the loop will run.
        break

# Done collecting gold!
hero.moveXY(58, 33)
# Go to Naria and say how much gold you collected.
hero.say(totalGold)

then, my hero immediately stops when she has 24, and then goes to Naria and tells her 26.

Iā€™ve checked a few posts but I couldnā€™t find the solution for this. Help me please.

I somehow managed to beat the level by eliminating all lines about ā€˜totalGoldā€™ and simply putting ā€˜hero.goldā€™, such as this below.

# Collect 25 gold, and then tell Naria the total.
# Use break to stop collecting when totalGold >= 25.

while True:
    coin = hero.findNearestItem()
    if coin:
        # Pick up the coin.
        hero.move(coin.pos)
        # Add the coin's value to totalGold.
        # Get its value with:  coin.value
        pass
    if hero.gold >= 25:
        # This breaks out of the loop to run code at the bottom.
        # The loop ends, code after the loop will run.
        break

# Done collecting gold!
hero.moveXY(58, 33)
# Go to Naria and say how much gold you collected.
hero.say(hero.gold)

But Iā€™m still wondering why did those happen.

I found it!!
The problem lay with the ā€˜moveā€™ command.
Iā€™m not sure this is a bug or not.
But ā€œhero.move(coin.pos)ā€ makes a lot of weird behaviours of the hero, while ā€œhero.moveXY(coin.pos.x, coin.pos.y)ā€ was just fine for any codes aboveā€¦
Could anyone please teach me what Iā€™m missing?
Why did this simple move command break the whole codes?

The Hints on this level give you some detail on how to approach this level correctly. Hint 2 specifies that you use moveXY(). The reason being, you want to pick up the coin before the next line of code runs.

    hero.moveXY(coin.pos.x, coin.pos.y) # moves to the XY coordinates before another line of code runs
    totalGold = totalGold + coin.value  # adds only the coin.value you just picked up
VS
    hero.move(coin.pos)  # takes one step and then runs the next line of code
    totalGold = totalGold + coin.value  # each step, you add the coin.value back into the total

One way you could make hero.move() work is adding a while loop before it. Essentially making it moveXY()

while distanceTo(coin) > 1:
    hero.move(coin.pos)

And hero.gold does the exact same thing that totalGold = totalGold + coin.value is doing.

1 Like

I appreciate it so much!!!