Help on Wishing Well. (Python) [Solved]

I can’t figure out why this isn’t working. My hero counts 104 coins, and then the code gets an error saying “coin is undefined.”

#  You need exactly 104 gold. 

less = "Nimis"
more = "Non satis"
requiredGold = 104

# This function calculates the sum of all coin values.
def sumCoinValues(coins):
    coinIndex = 0
    totalValue = 0
    # Iterate all coins.
    while coinIndex < len(coins):
        totalValue += coins[coinIndex].value
        coinIndex += 1
    return totalValue

def collectAllCoins():
    item = hero.findNearest(hero.findItems())
    while item:
        hero.moveXY(item.pos.x, item.pos.y)
        item = hero.findNearest(hero.findItems())

while True:
    items = hero.findItems()
    # Get the total value of coins.
    goldAmount = sumCoinValues(items)
    # If there are coins, then goldAmount isn't zero.
    if goldAmount != 0:
        # If goldAmount is less than requiredGold
        if goldAmount < requiredGold:
            # Then say "Non satis".
            hero.say("Non satis")
        # If goldAmount is greater than requiredGold
        if goldAmount > requiredGold:
            # Then say "Nimis".
            hero.say("Nimis")
        # If goldAmount is exactly equal to requiredGold
        if goldAmount == requiredGold:
            # Then collect all coins:
            hero.moveXY(coin.pos.x, coin.pos.y)
        pass

It’s this line: hero.moveXY(coin.pos.x, coin.pos.y) that always has the error. I do hero.moveXY(coin.pos.x, coin.pos.y) or hero.moveXY(coin.pos.x, coin.pos.y) and hero.move(item.pos). It always gets the same error!

Here’s the error I get.
image

Can you please use correct punctuation in your sentence because I don’t understand what you’re saying.

@Chaboi_3000? Can you help? (You were active a few minutes ago which is why I’m @-ing you)

you haven’t defined coin do this: coin = hero.findNearestItem()

Try to use the collectAllCoins() function if the gold amount is 104

Now I walk a few steps and stop and say Non satis.

# You need exactly 104 gold. 

less = "Nimis"
more = "Non satis"
requiredGold = 104

# This function calculates the sum of all coin values.
def sumCoinValues(coins):
    coinIndex = 0
    totalValue = 0
    # Iterate all coins.
    while coinIndex < len(coins):
        totalValue += coins[coinIndex].value
        coinIndex += 1
    return totalValue

def collectAllCoins():
    item = hero.findNearest(hero.findItems())
    while item:
        hero.moveXY(item.pos.x, item.pos.y)
        item = hero.findNearest(hero.findItems())

while True:
    items = hero.findItems()
    # Get the total value of coins.
    goldAmount = sumCoinValues(items)
    # If there are coins, then goldAmount isn't zero.
    if goldAmount != 0:
        # If goldAmount is less than requiredGold
        if goldAmount < requiredGold:
            # Then say "Non satis".
            hero.say("Non satis")
        # If goldAmount is greater than requiredGold
        if goldAmount > requiredGold:
            # Then say "Nimis".
            hero.say("Nimis")
        # If goldAmount is exactly equal to requiredGold
        if goldAmount == requiredGold:
            # Then collect all coins.
            coin = hero.findNearestItem()
            hero.moveXY(coin.pos.x, coin.pos.y)
        pass

Replace the

coin = hero.findNearestItem()
hero.moveXY(coin.pos.x, coin.pos.y)

with
collectAllCoins()

Thanks you so much! It worked.

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.