Hoarding Gold can't see issue in code

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

When using hero.move the character gets first coin right in front, then immediately breaks the loop, goes to the bottom XY coordinates and says “27” if I use Hero.moveXY and the full written coin.pos.x / y then it works.

I’m trying to determine why hero.move breaks it and especially where it’s getting a random “27” variable from.Preformatted text

Welcome‌ @Manticore412 ‌welcome‌ ‌to‌ ‌the‌ ‌forum!‌ ‌It‌ ‌is‌ ‌a‌ ‌lovely‌ ‌place‌ ‌where‌ ‌you‌ ‌can‌ ‌share‌ ‌all‌ ‌kinds‌ ‌of‌ stuff‌ ‌(appropriate‌ ‌of‌ ‌course),‌ ‌share‌ ‌bugs,‌ ‌and‌ ‌even‌ ‌get‌ ‌assistance‌ ‌for‌ ‌code!‌ ‌We‌ ‌suggest‌ ‌that‌ ‌you‌ ‌review‌ ‌this‌‌ topic‌ ‌which‌ ‌shows‌ ‌all‌ ‌essentials‌ ‌of‌ ‌this‌ ‌board!‌ ‌And‌ ‌we‌ ‌suggest‌ ‌you‌ ‌review‌ this topic‌ ‌which‌ ‌shows‌ ‌how‌ ‌to‌ ‌post‌ ‌your‌ ‌code‌ ‌correctly!‌ ‌Thanks!!‌ ‌ :partying_face: :partying_face:
Could you please post your code correctly so we can see the indents?
Thanks, Lydia

If you only do this, totalGold will only count the coin.value, so that’s why you’re getting 27.
This line right here should be
totalGold = coin.value + totalGold

For this level, I used moveXY.
Lydia

While troubleshooting the other part I also saw a reference about totalGold = totalGold + coin.value but then also found a video using the one from mine and the code works fine only changing the hero.move(coin.pos) to hero.moveXY(coin.pos.x, coin.pos.y)

It does work with that change, but I’m pulling out hair trying figure out why that breaks the loop and gives a high number when I’ve only collected a gold value of 3

So it breaks the loop, when the totalGold is more than or equal to 25, then it breaks the code.
Lydia

The loop is breaking at the first coin collected and a total gold shown as 3, let me know if you get the same results when you copy and paste my code. It’s in Sarven Desert 10lvls down the main path.

Can you send me your newest code?
Lydia

So, @Manticore412, I tried running your code and I found the issue, but it’s not something that should normally make your code not work. The only difference between our codes though was the exact same thing @Lydia_Song said:

This is your difference:

This what mine changed:

hero.moveXY(coin.pos.x, coin.pos.y)

It doesn’t make a lot of sense why this would change anything, because essentially they do the exact same thing. The only thing I can think of is that there might have be an issue internally, like when they were creating the level because they might have wanted you to only use hero.moveXY() in this level. Let me know if you have any further questions. Hope this helps! Grzmot

1 Like

I think Girzmot is right because you don’t learn move.pos until mountains.

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.