```
# 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:
coin = hero.findNearestItem()
# Then collect all coins:
hero.moveXY(coin.pos.x, coin.pos.y)
requiredGold -= 1
pass
```

Is my code. plz help

use your collectAllCoins() function

delete ths

1 Like

I don’t think it works.

Post your code

20 chars

```
# 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:
coin = hero.findNearestItem()
# Then collect all coins:
hero.moveXY(coin.pos.x, coin.pos.y)
requiredGold -= 1
pass
```

1 Like

You didn’t do the changes yet I told you

use your CollectAllCoins function

delete this

3 Likes

Like @Eric_Tang said, instead of moveXY statement, use collectAllCoins() function, as it will move to the nearest item.

1 Like

if you have not yet used `def`

…`def`

is a function that runs the whole code inside it, its a lot simpler than writing and rewriting it, type `collectAllCoins()`

and it will work. Since in your `collectAllCoins()`

function you have items delete this :

2 Likes

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