Question about my code

Hello,

I am at stuck at this level and i don’t understand where the problem comes from.
The level is “Wishing well”.

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

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

while True:
    items = hero.findItems()
    goldAmount = sumCoinValues(items)
    if len(items) != 0:
        # If there is not enough gold, then say "Non satis".
        if goldAmount < 104:
            hero.say("non satis")
        if goldAmount >104:
            hero.say("Nimis")
        if goldAmount == 140:
            coins = hero.findItems()
            coinIndex = 0
            nearest = coin
            nearestDistance = 9999
            while coinIndex < len(coins):
                coin = coins[coinIndex]
                coinIndex += 1
                distance = hero.distanceTo(coin)
                if distance < nearestDistance:
                    nearest = coin
                    nearestDistance = distance
                if nearest:
                    hero.moveXY(nearest.pos.x, nearest.pos.y)

if anyone is able to help me, it would be amazing !

Thank you,

Eliot

1 Like
  1. Are you sure the above statement is right?

  2. Also look at your loop that iterates through the array of coins. Are you looking to find the nearest coin or are you trying to just collect each coin? And if you are looking to find the nearest coin how many times would you need to do that process before you were able to collect all of the coins?

Wishing Well - link

1 Like

Thank you a lot.
I changed 140 by 104 and added a loop after if goldAmount == 104:

Now it is working :slight_smile:

Eliot

1 Like