# Unable to figure out diamond dozen

**URL:** <https://discourse.codecombat.com/t/unable-to-figure-out-diamond-dozen/27858>\
**Category:** Level Help\
**Created:** [March 10, 2021, 3:05am UTC](https://discourse.codecombat.com/t/unable-to-figure-out-diamond-dozen/27858 "2021-03-10T03:05:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elemersglue](https://avatars.discourse-cdn.com/v4/letter/e/b487fb/32.png) [@elemersglue](https://discourse.codecombat.com/u/elemersglue)\
**Post date:** [March 10, 2021, 3:05am UTC](https://discourse.codecombat.com/t/unable-to-figure-out-diamond-dozen/27858/1 "2021-03-10T03:05:34Z")

</div>

```python
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 valueOverDistance(item) > 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.move(coin.pos.x, coin.pox.y)

```

---

<div class="post-metadata">

**Author:** ![CocoCharlie](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cococharlie/32/19081_2.png) [@CocoCharlie](https://discourse.codecombat.com/u/CocoCharlie)\
**Post date:** [March 10, 2021, 3:40am UTC](https://discourse.codecombat.com/t/unable-to-figure-out-diamond-dozen/27858/2 "2021-03-10T03:40:15Z")

</div>

I’m on the same level. You can join this topic if you want.

> [@Diamond dozen(python)](https://discourse.codecombat.com/t/diamond-dozen-python/27814):
>
> This isn’t quite right. Please help! 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…

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [March 10, 2021, 8:01pm UTC](https://discourse.codecombat.com/t/unable-to-figure-out-diamond-dozen/27858/3 "2021-03-10T20:01:51Z")

</div>

Hi elemersglue,

While it’s nice of CocoCharlie to invite you to a discussion of his code, I think you’re probably after some hints about your code.

These lines here need some work:

```python
        if item.value/hero.distanceTo(item) < bestValue:
            bestItem = item
            bestValue = valueOverDistance(item)

```

Firstly have a think about the top line. If you had an item that had a value of 10 and was 1 metre away; and an item that had a value of 5 and was 5 metres away, then what would each of their ‘item.value/hero.distanceTo(item)’ be? Which one would be better? Which one would get set to the bestItem?

Once you’ve worked this one out, then the bottom of these three lines needs changing. Your valueOverDistance function doesn’t work. You can either fix it, or you can just put in the expression that you already have to find the item’s value divided by the hero’s distance to the item (which is…?).

Post again if you need more help.

Jenny
