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
My hero picks up more then 104 coins and it’s confusing me
also not sure how to format my code to fit properly on here
Mine too. Maybe because of boots or speed ring, idk. But I guess the level goal is completed? When gold field is changing at the start of the level, total gold amount message appears at the top of the map. When it says “104” hero starts to collect coins.
This topic may help a little ) [Essentials] How To Post/Format Your Code Correctly
# Claim the coins while defeating the marauding ogres.
def findMostHealth(enemies):
target = None
targetHealth = 0
enemyIndex = 0
while enemyIndex < len(enemies):
enemy = enemies[enemyIndex]
if enemy.health > targetHealth:
target = enemy
targetHealth = enemy.health
enemyIndex += 1
return target
def valueOverDistance(item):
return item.value / hero.distanceTo(item)
# Return the item with the highest valueOverDistance(item)
def findBestItem(items):
bestItem = None
bestValue = 0
itemsIndex = 0
# Loop over the items array.
# Find the item with the highest valueOverDistance()
while itemsIndex < len(items):
item = items[itemsIndex]
if item.value / hero.distanceTo(item) > bestValue:
bestItem = item
bestValue = valueOverDistance(item)
itemsIndex += 1
return bestItem
while True:
enemies = hero.findEnemies()
enemy = findMostHealth(enemies)
if enemy and enemy.health > 15:
while enemy.health > 0:
hero.attack(enemy)
else:
coins = hero.findItems()
coin = None
coin = findBestItem(coins)
if coin:
hero.moveXY(coin.pos.x, coin.pos.y)
thanks for showing me that. No i dont think its complete because it always says incomplete run out of time.
Ehm, looks like another level code) Not for “wishing well”.
It’s ok. And code too. I copypasted first post code and it works fine after formatting it.
And your formatted code post also completes the task well, I cheked.
Try another browser maybe?
you’re using python correct?
never mind it worked thank you
Yes, I play Python.
I simply delete all the code in editor, copied your formatted code from here and run it. Everything is ok.
Does your hero say “nimis” and “non satis” when you run code?
yep it works now thank you
1 Like
I’m glad to hear that)
So, now you may del the code from here as it is right solution.
Good luck)