# \[SOLVED\] Diamond dozen help (python)

**URL:** <https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104>\
**Category:** Level Help\
**Created:** [January 26, 2020, 8:14pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104 "2020-01-26T20:14:21Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![259222](https://avatars.discourse-cdn.com/v4/letter/2/dc4da7/32.png) [@259222](https://discourse.codecombat.com/u/259222)\
**Post date:** [January 26, 2020, 8:14pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/1 "2020-01-26T20:14:22Z")

</div>

My code isn’t working I don’t know what i’m doing wrong.

Here is my code:

```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()
    while itemsIndex<len(items):
        if items[itemsIndex].value>bestValue:
            bestItem=items[itemsIndex]
            bestValue=bestItem.value
        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:
            if hero.isReady("jump"):
                hero.jumpTo(coin)
            else:
                hero.moveXY(coin.pos.x, coin.pos.y)

```

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [January 26, 2020, 10:08pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/2 "2020-01-26T22:08:19Z")

</div>

@259222, welcome to the forum!

I combined the valueOverDistance function with findBestItem and came up with findOptimalCoin. In your code, you are not calling the valueOverDistanceFunction.

Figure out where/when you should call the value function, or possibly, try combining them…the calculation performed in the function is certainly handy.

---

<div class="post-metadata">

**Author:** ![259222](https://avatars.discourse-cdn.com/v4/letter/2/dc4da7/32.png) [@259222](https://discourse.codecombat.com/u/259222)\
**Post date:** [January 26, 2020, 10:37pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/3 "2020-01-26T22:37:02Z")

</div>

So I used your advice and this is my current code:

```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()
    while itemsIndex<len(items):
        if items[itemsIndex].value>bestValue:
            bestItem=items[itemsIndex]
            bestValue=bestItem.value
        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:
            valueOverDistance(coin)
            if hero.isReady("jump"):
                hero.jumpTo(coin)
            else:
                hero.moveXY(coin.pos.x, coin.pos.y)

```

But it still doesn’t work. Did I call the function in the wrong place?

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [January 26, 2020, 11:32pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/4 "2020-01-26T23:32:40Z")

</div>

Yes…it should actually be a clause in the ‘if items’ statement, in the findBestItem function.

I can’t figure out a good way to explain what/where to change your code, without just providing a solution…as I mentioned, I used a completely different method. I can PM you my code for that function, if you wish though.

---

<div class="post-metadata">

**Author:** ![259222](https://avatars.discourse-cdn.com/v4/letter/2/dc4da7/32.png) [@259222](https://discourse.codecombat.com/u/259222)\
**Post date:** [January 27, 2020, 12:16am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/5 "2020-01-27T00:16:55Z")

</div>

This is my current code:

```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()
    while itemsIndex<len(items):
        if items[itemsIndex].value>bestValue:
            valueOverDistance(coin)
            bestItem=items[itemsIndex]
            bestValue=bestItem.value
        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:
            if hero.isReady("jump"):
                hero.jumpTo(coin)
            else:
                hero.moveXY(coin.pos.x, coin.pos.y)

```

On the `return item.value / hero.distanceTo(item)` it says that “cannot read property ‘value’ of null”

what does that mean?

P.S. you said

> [@dedreous](#):
>
> I can PM you my code for that function, if you wish though.

what does ‘PM’ mean?

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [January 27, 2020, 10:52am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/6 "2020-01-27T10:52:29Z")

</div>

It means ‘private message’…this way, we avoid sharing a potential spoiler with everyone.

---

<div class="post-metadata">

**Author:** ![TinyGoliath06](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@TinyGoliath06](https://discourse.codecombat.com/u/TinyGoliath06)\
**Post date:** [August 12, 2020, 6:10pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/7 "2020-08-12T18:10:04Z")

</div>

Could someone please help me? This is my code.

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

```

It highlights item value in this phrase, return item.value / hero.distanceTo(item) , and says "cannot read property ‘value’ of undefined

---

<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:** [August 12, 2020, 6:46pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/8 "2020-08-12T18:46:33Z")

</div>

Compare how you write the function:

```python
def valueOverDistance(item):

```

with how you’re calling it:

```python
valueOverDistance()

```

What’s the difference?

Jenny

---

<div class="post-metadata">

**Author:** ![wazi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wazi/32/14115_2.png) [@wazi](https://discourse.codecombat.com/u/wazi)\
**Post date:** [September 21, 2020, 10:29am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/9 "2020-09-21T10:29:27Z")

</div>

hello I need help in this  
here is 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
    
    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:
            if hero.isReady("jump"):
                hero.jumpTo(coin)
            else:
              hero.moveXY(coin.pos.x, coin.pos.y)

```

---

<div class="post-metadata">

**Author:** ![wazi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wazi/32/14115_2.png) [@wazi](https://discourse.codecombat.com/u/wazi)\
**Post date:** [September 22, 2020, 9:31am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/10 "2020-09-22T09:31:10Z")

</div>

hello I’m back and I’ve change my code here it is

```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()
    
    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:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [September 22, 2020, 9:35am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/11 "2020-09-22T09:35:40Z")

</div>

Can you please try to format your code as it is described below so we will be able to help you solve the level?

> [@\[Essentials\] How To Post/Format Your Code Correctly](https://discourse.codecombat.com/t/essentials-how-to-post-format-your-code-correctly/15521):
>
> Formatting your code before posting code is essential to our community, and if you don’t we might not be able to help frowning Everyone must know how to format ALL of your code properly. It’s really easy and only requires a very small amount of effort. To post your code from the game , use the \</\> button or it won’t format properly. When you click the \</\> button, the following will appear: Please paste ALL …

Andrei

---

<div class="post-metadata">

**Author:** ![wazi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wazi/32/14115_2.png) [@wazi](https://discourse.codecombat.com/u/wazi)\
**Post date:** [September 22, 2020, 9:39am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/12 "2020-09-22T09:39:24Z")

</div>

I have formatted AnSeDra

---

<div class="post-metadata">

**Author:** ![wazi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wazi/32/14115_2.png) [@wazi](https://discourse.codecombat.com/u/wazi)\
**Post date:** [September 22, 2020, 9:52am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/14 "2020-09-22T09:52:03Z")

</div>

I figured it out, thank you!

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [September 22, 2020, 9:55am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/15 "2020-09-22T09:55:33Z")

</div>

Congratulations for completing the level!

Andrei

---

<div class="post-metadata">

**Author:** ![Mumbo\_6](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/mumbo_6/32/14166_2.png) [@Mumbo\_6](https://discourse.codecombat.com/u/Mumbo_6)\
**Post date:** [September 22, 2020, 9:56am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/16 "2020-09-22T09:56:03Z")

</div>

Congrats! 🥳 Please put a tick on your post so that people know that they can find a solution here

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [September 22, 2020, 9:57am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/17 "2020-09-22T09:57:43Z")

</div>

It is not @wazi’s topic, so he can not mark any post as the solution. But I edited the title to let the folks know that the topic is solved.

Andrei

---

<div class="post-metadata">

**Author:** ![Mumbo\_6](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/mumbo_6/32/14166_2.png) [@Mumbo\_6](https://discourse.codecombat.com/u/Mumbo_6)\
**Post date:** [September 22, 2020, 9:59am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/18 "2020-09-22T09:59:18Z")

</div>

Ok, thanks @AnSeDra!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [September 22, 2020, 9:59pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-help-python/20104/19 "2020-09-22T21:59:20Z")

</div>

Closed on September 22, 2020, 9:59pm UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
