# 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.

``totalGold = totalGold + coin.value;``
1 Like
#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.

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

#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

``````# 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

## 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â.