Need help with "Wishing Well" (Python)

I need some help, the problem is, I am able to get to where it is 104 coins, then he collects one coin, but that makes it less than 104, so he says “Non Satis” then just stays there until it runs out of time. Here is my code.

# 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".
        # If goldAmount is exactly equal to requiredGold
        if goldAmount == requiredGold:
            item = hero.findNearestItem()
            hero.moveXY(item.pos.x, item.pos.y)

Have any suggestions?

You need to set this up as a separate loop…making it a function would work well.

Sorry, still a bit confused, would I set up a while loop inside this loop?

If not inside, I don’t know what I would put inside that loop.

Create a new function containing the appropriate code to collect the coins. Then:

if goldAmount == requiredGold:
    call that new function

Or, you can wrap it in a while loop:

if goldAmount == requiredGold:
    item = find nearest
    while item exists:
        pick up item
        find next item
1 Like

It worked! Big thanks to @dedreous!

1 Like

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