Hoarding gold not working


#1

I am not able to get the code for hoarding gold to work. I have tired various bits of code to tell Naria how much. Everytime it tells her 25 when I collect 27 or another random number. Suggestions as to what I might be doing wrong?

code

totalGold = 0
loop:
    coin = self.findNearestItem()
    if coin:
        # Pick up the coin.
        coinPos = coin.pos
        cx = coin.pos.x 
        cy = coin.pos.y 
        self.moveXY(cx, cy)
        totalGold += coin.value
        # Add the coin's value to totalGold. (See the guide for more.)
        # Get its value with:  coin.value

        pass
    
    if totalGold >= 25:
       self.say ("I'm Done Collecting gold")

self.moveXY(58, 33)

self.say ("I have collected  " + totalGold + " gold!")

I have tried both the above and also

self.say(total.Gold)

#2

First, there is no “.”, you must say:

self.say(totalGold)

Second, this way of counting gold will fail if you run over another coin while going to the first one.
But for this level, should not be a problem - you should collect the coins one by one.

Debug your code by saying the total gold after each coin you collect. check where the difference appears:

if coin:
    pickup coin
    totalGold+=coin.value
    self.say(totalGold)

#3

The period in the self.say(total.Gold) was a miss print on my part.

I added a self.say(totalGold) statement after picking up the coin and it stopped the program and said that I didn’t have enough gold. Do I need to not run the last part of the program where I am telling Naria how much gold I have?

code:

loop:
    coin = self.findNearestItem()
    if coin:
        # Pick up the coin.
        coinPos = coin.pos
        cx = coin.pos.x 
        cy = coin.pos.y 
        self.moveXY(cx, cy)
        totalGold += coin.value 
        # Add the coin's value to totalGold. (See the guide for more.)
        # Get its value with:  coin.value

        pass
    
    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 ("I'm Done Collecting gold")
# Done collecting gold!
self.moveXY(58, 33)
# Go to Naria and say how much gold you collected.
self.say (totalGold)

#4
  1. Uups, on this level as soon as you say something, Naria will believe you are done.

Ok, to debug it, take out the say in the loop (sorry about it) but change the value of totalGold>=25
to something totalGold>=5, run the program, then totalGold>=10 run the program …

Check your real total gold collected in the top-right of the screen and try to determine when the error occurs (when the totalGold becomes different from the total gold collected)

  1. Delete the pass you don’t need it. Pass is used only during code development, when you plan to do something but you didn’t got around to do it yet. For example, an if expects at least one instruction after it:
if enemy:
    #I'll write code for it later, but to make the compiler happy now I add
    pass
else:
    self.collectCoins()

#5

So what I find is that when I run a the code with totalGold >= 5 it collets 9 and says 6 taking 4 coins. When I run it less than 5 it reports the correct number of coins/gold collected.
The coin tips collected are
1= gold
1= bronze
1= silver
1= gold

Is it something to do with this part of my code:

 self.moveXY(cx, cy)
        totalGold += coin.value 

Do I need to 1 up the coin.value part since it seams I am 1 off the count?


#6

Can you past the last version of your code? I’ll run it and figure out where is the issue


#7

Here is what I have without the say how much loop:

totalGold = 0
loop:
    coin = self.findNearestItem()
    if coin:
        # Pick up the coin.
        coinPos = coin.pos
        cx = coin.pos.x 
        cy = coin.pos.y 
        self.moveXY(cx, cy)
        totalGold += coin.value 
        # Add the coin's value to totalGold. (See the guide for more.)
        # Get its value with:  coin.value

    if totalGold >= 5:
        # >= means totalGold is greater than or equal to 25.
        # This breaks out of the loop to run code at the bottom.
        break
self.say ("I'm Done Collecting gold")
# Done collecting gold!
self.moveXY(58, 33)
# Go to Naria and say how much gold you collected.
self.say (totalGold)

#8

Any thoughts on what might be wrong?


#9

Just delete the:
self.say (“I’m Done Collecting gold”)

and it will work.

What happens: when you issue a move command, your character is on the move.
Saying something you’ll make you lose control of your character for 1 second (saying takes time).
During this time the character will “slide” in the direction of the move and bump into the next coin.

Sorry for missing the earlier post. I do not know why it had not appeared on my list.