[SOLVED] Hoarding Gold (Python)

Hello, it’s been hours and i can’t slove this level my hero would go collect all the coins then go to naria and say he got 27 coins.

# 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.moveXY(coin.pos.x, coin.pos.y)
        # 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)

I just want to know what’s wrong with my code, i read all other threads but nothing helped me.

1 Like

You’ve not assigned a value to totalGold. Towards the end of your first if conditional statement, you have totalGold += coin.value. This adds the two together but doesn’t store that sum anywhere to pass to the next if conditional. If you change that to totalGold = totalGold + coin.value it should work.

i actually tried to put

totalGold = totalGold + coin.value

instead of

totalGold += coin.value

but that only made him collect 43 coins.

What equipment are you using? Please post a screenshot of it. Thanks.

%D8%A7%D9%84%D8%AA%D9%82%D8%A7%D8%B7

It’s an equipment issue. Use any pet other than the one you’re using.

Thank you very much :smiley:

I am stuck on my armor can I get you in this clan https://codecombat.com/clans/5d542f59e71853002410b9a1

I need help here is my code :disappointed_relieved:
// Collect 25 gold, and then tell Naria the total.
// Use break to stop collecting when totalGold >= 25.

var totalGold = 0;
while(true) {
    var coin = hero.findNearestItem();
    if(coin) {
        // Pick up the coin.
        hero.moveXY(coin.pos.x,coin.pos.y);
        // Add the coin's value to totalGold.
        // Get its value with:  coin.value
        var totalGold = 25;
    }
    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("hi Naria 25 gold");

Instead of this, put totalGold in the hero.say

1 Like

So did it work and completed the level?

1 Like

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