CodeCombat - Wishing Well

Could someone please help me with this level? 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".
            hero.say("Nimis")
        # If goldAmount is exactly equal to requiredGold
        if goldAmount == requiredGold:
            # Then collect all coins:
            coin = hero.findItems()
            if coin:
                hero.moveXY(coin.pos.x, coin.pos.y)
        pass

Thank you in advance!

(I don’t really think anyone’s going to reply because it’s the weekend)

Oh, well. (20 characters)

People are on in the weekend. You’ve only given it 10 min. Have patience.

# Then collect all coins
Your code here makes no effort to collect all coins. You have a defined function collectAllCoins, perhaps use that?

(And sidenote- you’ve been extraordinarily impatient when it comes to answers. More messages never help, and it’s unreasonable to always expect a reply in under a day, let alone a couple of minutes.)

1 Like

You only want to check if it is greater than or less than.

If the goldAmount is equal to the requiredGold, then use the collectAllCoins() function to collect the coins.

Okay. First of all, the “collectAllCoins()” function didn’t work. My guy collected all the coins, but it was more than 104, and I didn’t complete the level. Second of all, I wasn’t being impatient. I was just saying that, since it was the weekend, you guys didn’t have to reply to me.

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".
            hero.say("Nimis")
        # If goldAmount is exactly equal to requiredGold
        if goldAmount == requiredGold:
            # Then collect all coins:
            collectAllCoins()
            pass

can you send the link?

Sure. Here it is: CodeCombat - Coding games to learn Python and JavaScript

You are collecting the coins at the wrong time because because of these two statements:

and

You should not be checking if they are equal to. They need to be either greater than or less than. If you check if they are greater than or equal to (or less than or equal to), then you will collect the coins at the wrong time. Those two statements should be:

and

Thank you! It worked!

1 Like

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