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.
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â.
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
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!")
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
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:
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!
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.
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)
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?
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)
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â.