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 findOptimalCoin(coins):
coinIndex = 0
coinValue = 0
coinDistance = 0
Optimalcoin = 0
while len(coins) > coinIndex:
coin = coin[coinIndex]
if coin.value/hero.distanceTo(coin) > Optimalcoin:
coinDistance = hero.distanceTo(coin)
coinValue = coin.value
Optimalcoin = coinValue/coinDistance
coinIndex += 1
return Optimalcoin
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 = findOptimalCoin(coins) # ∆ Uncomment this once you've written the function.
if coin:
hero.moveXY(coin.pos.x, coin.pos.y)
I keep getting an error in the while loop line of the findOptimalCoins function.
“Type error: can’t acces get 0 of that type: undefined”