# Diamond dozen (python)

**URL:** <https://discourse.codecombat.com/t/diamond-dozen-python/13320>\
**Category:** Uncategorized\
**Created:** [December 20, 2017, 4:14pm UTC](https://discourse.codecombat.com/t/diamond-dozen-python/13320 "2017-12-20T16:14:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Atticus\_Black](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Atticus\_Black](https://discourse.codecombat.com/u/Atticus_Black)\
**Post date:** [December 20, 2017, 4:14pm UTC](https://discourse.codecombat.com/t/diamond-dozen-python/13320/1 "2017-12-20T16:14:28Z")

</div>

my code:

```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 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()
    findMostValueOverDistance
    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)

```

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [December 20, 2017, 5:23pm UTC](https://discourse.codecombat.com/t/diamond-dozen-python/13320/2 "2017-12-20T17:23:05Z")

</div>

I strongly suggest that you use the search feature and read previous threads about this level. I was stuck on it for a couple of days myself. Your code needs some work and it’s best for you to do some research to learn what needs to be done.

---

<div class="post-metadata">

**Author:** ![SuperSmacker](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/supersmacker/32/8385_2.png) [@SuperSmacker](https://discourse.codecombat.com/u/SuperSmacker)\
**Post date:** [December 20, 2017, 11:18pm UTC](https://discourse.codecombat.com/t/diamond-dozen-python/13320/3 "2017-12-20T23:18:48Z")

</div>

gg on mod, @MunkeyShynes

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [December 21, 2017, 12:05am UTC](https://discourse.codecombat.com/t/diamond-dozen-python/13320/4 "2017-12-21T00:05:30Z")

</div>

@SuperSmacker  
Thanks. It seems I spend half my life here anyway. LOL.

---

<div class="post-metadata">

**Author:** ![MunkeyShynes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munkeyshynes/32/9277_2.png) [@MunkeyShynes](https://discourse.codecombat.com/u/MunkeyShynes)\
**Post date:** [December 21, 2017, 4:15pm UTC](https://discourse.codecombat.com/t/diamond-dozen-python/13320/5 "2017-12-21T16:15:22Z")

</div>

@Atticus_Black

I found this and thought it might help.

 ![PythonPosterOriginal_Page15](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/f/fa3def3f22d92c7b5a219c81d1e918446133f234.jpg)

---

<div class="post-metadata">

**Author:** ![maka](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/maka/32/3261_2.png) [@maka](https://discourse.codecombat.com/u/maka)\
**Post date:** [December 21, 2017, 5:32pm UTC](https://discourse.codecombat.com/t/diamond-dozen-python/13320/6 "2017-12-21T17:32:03Z")

</div>

Those are good, though we’re replaced ‘self’ with ‘hero’.

---

<div class="post-metadata">

**Author:** ![Atticus\_Black](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Atticus\_Black](https://discourse.codecombat.com/u/Atticus_Black)\
**Post date:** [January 4, 2018, 1:28pm UTC](https://discourse.codecombat.com/t/diamond-dozen-python/13320/7 "2018-01-04T13:28:03Z")

</div>

i tried those could you check my code and see what I would put for the missing segment

```python
# 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()
    
    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)

```

---

<div class="post-metadata">

**Author:** ![SuperSmacker](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/supersmacker/32/8385_2.png) [@SuperSmacker](https://discourse.codecombat.com/u/SuperSmacker)\
**Post date:** [January 4, 2018, 5:15pm UTC](https://discourse.codecombat.com/t/diamond-dozen-python/13320/8 "2018-01-04T17:15:24Z")

</div>

```python
i = 0
greatestValue = 0
for item in item:
    value = valueOverDistance(item)
    # each time you go through the loop, compare the current item's value to the max value.
    if value > greatestValue:
        value = greatestValue
        index = i
        # store the index of the item as well, so you can return the index and use it in the main loop to find and collect the item.
        # index is i in this case.
        i += 1
    else:
        i += 1
return index

```

---

<div class="post-metadata">

**Author:** ![Atticus\_Black](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@Atticus\_Black](https://discourse.codecombat.com/u/Atticus_Black)\
**Post date:** [January 18, 2018, 1:18pm UTC](https://discourse.codecombat.com/t/diamond-dozen-python/13320/9 "2018-01-18T13:18:42Z")

</div>

thank you for the help
