Help on Level: Hoarding Gold


#1

I have tried this level, but always get too much gold. Here is my code:

// Collect 25 gold, and then tell Naria the total.
// Use break to stop collecting when totalGold >= 25.
var totalGold = 0;
loop {
    var coin = this.findNearestItem();
    // Pick up the coin and add its value to the total.
    // Get its value with:  coin.value
    if (coin) {
        x = coin.pos.x;
        y = coin.pos.y;
        this.moveXY(x, y);
        totalGold = totalGold + 1;
    }
    
    if (totalGold >= 25) {
        // >= means totalGold is greater than or equal to 25.
        // This breaks out of the loop to run code at the bottom.
        break;
    }
}

this.say("Done collecting gold!");
// Go to Naria and say how much gold you collected.

This works pretty well, except for one little problem. I can’t figure out how to differentiate between the copper, silver, and gold coins, and they give different values. So, my code that tells my hero to add one to his mental gold value only works if all of the coins are copper, which they aren’t, so I was wondering if anyone could tell me how to tell the difference between the coins.


#2

Remember that coins have different values.

Try adding something like this:

totalGold = totalGold + coin.value;

#3

My understanding from the multiplayer level is that the property you are looking for is “bountyGold”. So after you have your “coin” variable with the nearest item, you can change “totalGold = totalGold + 1” to “totalGold = totalGold + coin.bountyGold”.

On an unrelated note, in Javascript you can also use the “+=” assignment operator which says add the next value to my current value - so it would work just as well to do “totalGold += coin.bountyGold”.


#4

in this level its “coin.value” not bountyGold. They say it specifically in the default comments


#5

I got it. thanks tjchan


#6

Yes! Thanks for the reminder. That is a more simplified way of doing it. =D I need to start using the += -= notation.


#7

Hey guys,

I also need some help in this level. I don’t know why, but my character stops just before getting the 25th gold coin.

This is what I have.

totalGold = 0

loop:
    coin = self.findNearestItem()
    totalGold = totalGold + coin.value
    
    if totalGold >= 25:
        # >= means totalGold is greater than or equal to 25.
        # This breaks out of the loop to run code at the bottom.
        break
        self.moveXY(59, 33)
        self.say("Done collecting gold!")
        pass

    else:
        pos = coin.pos 
        x = coin.pos.x
        y = coin.pos.y
        self.moveXY(x, y)
        pass

#8

I pass through! Thanks anyways!


#9

Just for anyone wondering:

The

if totalGold >= 25:
    #...
    break
    ...

was the problem. He breaked out of the loop, completly ignoring the move- and say-command. This can be solved by moving the lines behind the loop like this:

loop:
    ...
    if totalGold >= 25:
        break
    else:
        ...

#This is not in the loop anymore, because it is not intended
self.moveXY(59, 33)
self.say("Done collecting gold!")

#10

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

Having trouble with this mission. Basically my character will collect 24 gold, then go over to Naria and say she has 26 gold. I’ve tried with different variables, and it always ends up being that she says she has 2 additional gold that she does not actually have. Can anyone tell me what is wrong with my code? It’s Python.

totalGold = 0
loop:
Pick up the coin and add its value to the total.
Get its value with: coin.value
coin = self.findNearestItem()
totalGold = totalGold + coin.value
if totalGold <= 25:
self.moveXY(coin.pos.x, coin.pos.y)
else:
break

self.say(“Done collecting gold!”)
self.moveXY(59, 33)
self.say(totalGold)

sorry I don’t know how to preserve the spacing for the code, this whole coding thing is very new to me.


#11

So from the FAQ on the Adventure page. Feel free to repost your new code.

Post your code with radiant, harmonious formatting

You can get your code to format properly by surrounding it in triple backticks. (```) This:

enemy = self.findNearestEnemy()
if enemy:
self.attack(enemy)

becomes this:

enemy = self.findNearestEnemy()
if enemy:
    self.attack(enemy)

#12

Hi!

The first thing I might suggest is that you consider rearranging the first piece of your code into a more linear logical order :slight_smile: Something like: first, set totalGold = self.gold; then, if totalGold is less than 25, find the nearest coin, pick it up, and add its value to totalGold; else, break out of the loop.

For the last line once you’ve walked over to Naria, I might suggest trying this:

self.say("Done collecting gold!")
self.moveXY(59, 33)
self.say("I collected " + str(totalGold) + " gold!")

The str() will convert your totalGold variable into a string; the + signs concatenate them into a single sentence. Don’t forget to include the spaces! :slight_smile:


#13
totalGold = 0
loop:
    # Pick up the coin and add its value to the total.
    # Get its value with:  coin.value
    coin = self.findNearestItem()
    totalGold = totalGold + coin.value
    if totalGold <= 25:
        self.moveXY(coin.pos.x, coin.pos.y)
    else:
        break

self.say("Done collecting gold!")
self.moveXY(59, 33)
self.say(totalGold)

Thanks for the tip! Here is how my code is sitting now. I would be grateful if this could be explained in Python terms, because I have no experience with JavaScript. I understand that they have equivalent terms but I’m struggling enough as it is without having to translate from JavaScript to Python, and it would be much appreciated.

Another question: are there certain things that can always be added to a term if it allows it? Like for example, if I define something (x = whatever) can I always put x.value to derive value of x if it is a numerical thing, or x.pos, if it is on a positional grid?

Haha I hope that made sense. I’m kind of grasping for the best way to explain what I’m talking about here.


#15

Put in this code and it fixed it somehow. No quite sure why it worked or what the problem was before, but it’s flawless now! Thanks for the suggestions everyone :smile:

# Collect 25 gold, and then tell Naria the total.
# Use break to stop collecting when totalGold >= 25.
totalGold = 0
loop:
    coin = self.findNearestItem()
    totalGold = coin.value + totalGold
    if totalGold <= 25:
        self.moveXY(coin.pos.x, coin.pos.y)
        
            
    # Pick up the coin and add its value to the total.
    # Get its value with:  coin.value
    
    
    if totalGold >= 25:
        # >= means totalGold is greater than or equal to 25.
        # This breaks out of the loop to run code at the bottom.
        break

self.say("Done collecting gold!")
self.moveXY(59, 33)
self.say(totalGold)

#16

For some reason, it keeps telling me this!

Line 5: undefined is not a function (evaluation ‘tmp8tmp9’)


#17

Nevermind, silly me. I wasn’t wearing the glasses that grant you the special skill to find the nearest item! LOL!


#18

I tried this code. I didn’t have enough time to reach a person gives a message.
how can I run fast or have enough time?


#20

I followed the help video and typed in exactly what the person in the video did and it did not work. I had to add another coin.value after the break, otherwise my character would say the incorrect amount of gold. Can somebody explain to me why the video’s code didn’t work for me, but it did after I added the additional totalGold += coin.value? Here is my code, made bold is what I added that differs from the video’s code. Again, without the added part in the video, it worked for that person, but did not work for me and I don’t know why. I can see that the coin.value would be lost in the loop after the break, so I can see why I would need to find it again after the break. But, why didn’t the video maker need to?

totalGold = 0
loop:

coin = self.findNearestItem()
coinPos = coin.pos
coinx = coinPos.x 
coiny = coinPos.y 
self.moveXY(coinx, coiny)
totalGold += coin.value

if totalGold >= 25:
    break

totalGold += coin.value
self.say(“Done collecting gold!”)
self.moveXY(58, 33)
self.say(“I’ve collected " + totalGold + " gold”)


#21

The best way to learn is to just play around with the code to see how you can get the different values. I found the solution below.
The reason that the totalGold is less than actual gold is due to not adding the last coin you picked up.

It is because you are using a Loop statement

if you were to go through the loop one more time you would have the correct answer but the wrong value as you would have added the previous coin to your total gold, but also picked up another coin before answering.

totalGold = 0
self.moveXY(12, 35)
loop:
coin = self.findNearest(self.findItems())
totalGold = self.gold + coin.value
# Pick up the coin.
for item in coin:
self.moveXY(coin.pos.x, coin.pos.y)
# Add the coin’s value to totalGold. (See the guide for more.)

    # Get its value with:  coin.value
    pass
elif self.gold >= 25:
    # >= means totalGold is greater than or equal to 25.
    # This breaks out of the loop to run code at the bottom.
    if self.gold >= 25:
        break
        self.moveXY(coin.pos.x, coin.pos.y)
    self.moveXY(64, 33)  

self.say(“How much gold you collected?”)
self.say("I have " + totalGold)


#22

Just out of curiosity, would it be possible to answer this exercise with an if/elif condition versus and if if setup? Silly question, but when I think about it, it seems like it is possible. I have putted around with some different ideas and I feel like I am close. I am not looking for someone to give me an answer, but rather to say “yes it is possible”, or “no, it is not”.