Hoarding Gold (No coin.value code?)

Hey guys I need help on hoarding gold with coin.value. This is my code so far and I’m using quartz sense stone. Everytime I use the code it says coin.value is unavailable.

# Use break to stop collecting when totalGold >= 25
totalGold = totalGold + coin.value
loop:
    coin = self.findNearestItem()
    if coin:
        pos = coin.pos
        itemX = pos.x
        itemY = pos.y
        self.moveXY(itemX, itemY)
        totalGold = totalGold + 1

    # Pick up the coin and add its value to the total.
    # Get its value with:  coin.value
    
    
    if totalGold >= 25:
1 Like

You are trying to use coin before its defined (inside the loop). You can’t do this. Also your totalGold line inside the loop is wrong.

change this

to

totalGold += coin.value

of

totalGold = totalGold + coin.value

and remove the line above the loop. and it should be good (provided theres nothing else wrong)

1 Like

So is this correct

loop:
    coin = self.findNearestItem()
    if coin:
        pos = coin.pos
        itemX = pos.x
        itemY = pos.y
        self.moveXY(itemX, itemY)
        totalGold = totalGold + coin.value
        totalGold += coin.value
        


    # Pick up the coin and add its value to the total.
    # Get its value with:  coin.value
    
    
    if totalGold >= 25:
1 Like

no. you just need one of those lines. pick one they do the same thing. btw. to preserve spacing of code block put three ` characters on a line of its own both before and after the code block.

1 Like
# Collect 25 gold, and then tell Naria the total.
# Use break to stop collecting when totalGold >= 25.
totalGold = 0
loop:
    coin = self.findNearestItem()
    if coin:
        pos = coin.pos
        itemX = pos.x
        itemY = pos.y
        self.moveXY(itemX, itemY)
        totalGold = totalGold + coin.value
        
    # Pick up the coin and add its value to the total.
    # Get its value with:  coin.value
    
    
    if totalGold >= 25:
        self.moveXY(59, 33)
        self.say("Done collecting gold")
        break
    
    

# Go to Naria and say how much gold you collected.

^My code.
It works, but it won’t finish. I go over to Naria and tell her I finished collecting, yet it doesn’t end.
Some help please?

1 Like

Move

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

outside of your ‘if’ statement, and also you’ll want to tell Naria how much gold you’ve collected, not just that you’ve finished collecting gold. Make sure you do that. :smile:

1 Like

Thank you for the help!

1 Like

Here’s my code, it works everytime:

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

loop {
    var coin = this.findNearest(this.findItems());
    // Pick up the coin and add its value to the total.
    // Get its value with:  coin.value
    var cY = coin.pos.y;
    var cX = coin.pos.x;
    this.moveXY(cX, cY);
    var totalGold = 0 + this.gold;
    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.moveXY(59, 33);
this.say(this.gold);

I didn’t use “coin.value” like the instructions said because that caused an error to pop because I used the word “coin” to define my x and y values for the coins; in coin.pos.x and coin.pos.y. I also used “this.gold” to add to totalGold, for the if statement, which I got from the item description for “gold” under the quartz sense stone. Lastly, I couldn’t get my character to say the right amount of gold when I clicked “submit” to finish the level. I first started using “this.say(“25 gold”);” Since the final submission of your code generates the level at random I would pick up more than 25 gold and the level wouldn’t finish properly. I discovered if you type "this.say(this.gold); your character says whatever amount of gold they have at the time.

1 Like

As an little optimization:

Instead of

var totalGold = 0 + this.gold;
if (totalGold >= 25) {...}

you can directly say

if (this.gold >= 25) {...}

This is a bit better to understand for another person. If I look at code, I always check if/while/for ect. first when debugging, and this way I can see that you use the unmodified goldValue. totalGold can be anything.
But that’s just me nitpicking.

5 Likes

Agreed! And to further expand on optimization, it can also be used for POS positions.

Instead of

EX = enemy.pos.x
EY = enemy.pos.y
moveXY(EX, EY)
attack(enemy)

one could simply say

moveXY(enemy.pos.x, enemy.pos.y) 
attack(enemy)
1 Like

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, UNLESS I say “if total gold = <= 0” then she will walk into the gold between the start and Naria, and say she has 3. Can anyone tell me what is wrong with my code?

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)
1 Like

sorry I couldn’t figure out how to preserve the codes spacing format

1 Like

Fixed it.
You preserve the whitespaces by placing backticks like this (I used a trick to display these backticks, normally they would disappear and be replaced by the codeblock):

```
#your code
```

would become

#your code
1 Like

You need to collect the last coin instead of breaking when it would bring you over 25, since you already added it to your total. Break after you collect it.

1 Like

我是我的代码,有时候会失败,有时候会成功,coin.value这个怎么理解?

# Collect 25 gold, and then tell Naria the total.
    #收集25枚金牌,然后告诉Naria总数。
    # Use break to stop collecting when totalGold >= 25. 
    #用刹车时要停止收集totalGold>=25。
    totalGold = 0
    loop:
        coin = self.findNearestItem()
    # Pick up the coin and add its value to the total.
    #拾起硬币并添加其总价值。
    # Get its value with:  coin.value
    #获取其值:coin.value
    
        self.moveXY(coin.pos.x, coin.pos.y)
        totalGold = self.gold
        
        
        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!")
    # Go to Naria and say how much gold you collected.
    self.moveXY(59, 33)
    self.say("collect 25 gold")
1 Like

You have to say how much gold you actually collected, not 25, since sometimes you’re a bit over by picking up a bigger coin.

1 Like

如何声明我收集了多少金子?是否可以在say里面直接写totalgold变量?

1 Like

那么代码改成self.say(totalgold)就是正确答案了!!谢谢了

self.say("Done collecting gold!")
# Go to Naria and say how much gold you collected.
self.moveXY(59, 33)
self.say(totalGold)
1 Like

This code works for Python Users:

loop:
    coin = self.findNearest(self.findItems());
    cY = coin.pos.y
    cX = coin.pos.x
    self.moveXY(cX, cY)
    totalGold = 0 + self.gold
    if (totalGold >= 25):
        self.say("Done collecting gold!");
        self.moveXY(59, 33);
        self.say(self.gold);
1 Like

The code works even better now, after I put backticks around. Please, read the FAQ (pinned on your front page)!

1 Like