Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[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!!!