I've put diamond dozen off and now I can't crack it

Yeah. Kinda embarrassing, but here’s my code.


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
    itemIndex = 0
    items = hero.findItems()
    item = items[itemIndex]
    # Loop over the items array.
    # Find the item with the highest valueOverDistance()
    if valueOverDistance(item) > bestValue:
        bestValue = item.value
        bestItem = item
        itemIndex += 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)

And here’s the error

Can I have a link please

You’re very close. You’re only actually missing one line, and you need some extra indentation as well.
You’re missing a while loop line in the findBestItem(items) function. You should use the same format as what you used in the findMostHealth function, but only replacing enemies and enemyIndex with items and itemIndex.

Put this in the item function with item instead of enemy and items instead of enemies and then change the indentation of the lines to match the first function as well.
Milton, you can just add the name of the level to this link:
https://codecombat.com/play/level/
With a hypen between the words. So it would be diamond-dozen. Meaning the full link would be:
https://codecombat.com/play/level/diamond-dozen

1 Like