# \[Solved\] Diamond Dozen Python help

**URL:** <https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295>\
**Category:** Level Help\
**Created:** [December 17, 2017, 5:31am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295 "2017-12-17T05:31:29Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hexagon2059662](https://avatars.discourse-cdn.com/v4/letter/h/e99b99/32.png) [@Hexagon2059662](https://discourse.codecombat.com/u/Hexagon2059662)\
**Post date:** [December 17, 2017, 5:31am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/1 "2017-12-17T05:31:30Z")

</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(coin):
    return coin.value / hero.distanceTo(coin)

# 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(coin)
    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)

```

Is there anything wrong with my code? Is it just the varibles?

---

<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 17, 2017, 5:37am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/2 "2017-12-17T05:37:12Z")

</div>

findBestItem part is incorrect.

valueOverDistance(coin) just gives the value / distance

> [@Hexagon2059662](#):
>
> ```python
> # Loop over the items array.
> # Find the item with the highest valueOverDistance()
> 
> ```

Do that.

---

<div class="post-metadata">

**Author:** ![Hexagon2059662](https://avatars.discourse-cdn.com/v4/letter/h/e99b99/32.png) [@Hexagon2059662](https://discourse.codecombat.com/u/Hexagon2059662)\
**Post date:** [December 19, 2017, 2:33am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/3 "2017-12-19T02:33:14Z")

</div>

```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):
        item = items[itemIndex]
        if item.value / hero.distanceTo(item) > bestValue:
            bestValue = item
            bestitem = 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.moveXY(coin.pos.x, coin.pos.y)

```

My guy only attacks the first wave of ogres and nothing else.

---

<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 19, 2017, 3:18am UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/4 "2017-12-19T03:18:04Z")

</div>

A couple of things:

```python
        item = items[itemIndex]

```

The itemsIndex starts at 0. item **s** Index. Not itemIndex. You forgot the “S”.

Next, you have the assignments of bestItem and bestValue reversed.

---

<div class="post-metadata">

**Author:** ![Angad\_Tendulkar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/angad_tendulkar/32/9995_2.png) [@Angad\_Tendulkar](https://discourse.codecombat.com/u/Angad_Tendulkar)\
**Post date:** [May 5, 2019, 6:38pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/5 "2019-05-05T18:38:28Z")

</div>

this isn’t working

```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):
        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.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:** [May 5, 2019, 6:54pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/6 "2019-05-05T18:54:29Z")

</div>

Works for me. Try upgrading your equipment

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [May 5, 2019, 7:24pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/7 "2019-05-05T19:24:53Z")

</div>

> [@Diamond Dozen always non optimal](https://discourse.codecombat.com/t/diamond-dozen-always-non-optimal/14554/5):
>
> Your code runs successfully with my equipment after replacing the last row hero.moveXY(coin.pos.x, coin.pos.y) with hero.move(coin.pos) I always replace moveXY with move when possible. MoveXY has a hidden loop inside it and it’s blocking or slowing some actions

---

<div class="post-metadata">

**Author:** ![Angad\_Tendulkar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/angad_tendulkar/32/9995_2.png) [@Angad\_Tendulkar](https://discourse.codecombat.com/u/Angad_Tendulkar)\
**Post date:** [May 24, 2019, 6:31pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/8 "2019-05-24T18:31:56Z")

</div>

If you want I can give a screenshot of the hero’s equipment

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/c/cd9675f697d9981b52e5dccb659463787dcf41ff.jpeg)

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [May 24, 2019, 9:01pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/9 "2019-05-24T21:01:56Z")

</div>

using movXY you command your hero to move at the exact coin position.

```python
hero.moveXY(coin.pos.x, coin.pos.y)

```

replace the action with

```python
hero.move(coin.pos) 

```

and you will grab the coins at arm’s length , so more quickly.  
And your boots are really slow, I had made the same mistake buying them. If you insist using moveXY replace them with 2m/s speed boots.

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [May 24, 2019, 9:33pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/10 "2019-05-24T21:33:44Z")

</div>

Please explain how it isn’t working. I grabbed the same hero/gear and was able to pass the level. Oddly enough, even when I improved the gear to boots with 2m/s and even the speed ring, the level still took as long.

@xython The `hero.moveXY()` is part of the starter code. While it can be changed, I don’t know how much it would help on this level. There is a delay between the coin spawn events that makes faster gear minimally more beneficial.

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [May 24, 2019, 9:40pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/11 "2019-05-24T21:40:17Z")

</div>

Try and see if there’s a difference 🙂  
/Edit - test with slower free hero/

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [May 25, 2019, 1:03pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/12 "2019-05-25T13:03:06Z")

</div>

I see what you mean. The hero picks up the item before you actually reach the XY position and the move method will switch to the next item as soon as the original item is picked up while the moveXY will still continue to move to the exact XY. I was able to pick up two items on some of the spawn groups even with the slowest boots and slower free hero. I’m wondering if the moveXY has a slight delay to move to the next item because the hero never even hinted that there was a second item.

Hero: Lady Ida Justheart  
boots: Boots of Jumping speed 1.5m/s  
no speed ring

Two images below show the move option is quite better. The hero grabbed the gem and did pick up the Gold coin too. While the moveXY only goes to the gem.

![two%20for%20one%201](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/4/479a40015659ec87e78795c9cdb72c0889344401.jpeg) ![two%20for%20one%202](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/b/b65cad15a533dd087c1314dfeb9713649c1d5d3a.jpeg)

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [May 25, 2019, 4:50pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/13 "2019-05-25T16:50:47Z")

</div>

I tested the level with same boots ( 1.5m/s) and an ordinary hero, but with speed ring, no Boss Star. Used also the function moveToXY from [Grab coins with moveXY almost as with move boots](https://discourse.codecombat.com/t/grab-coins-with-movexy-almost-as-with-move-boots/18508) . So the gold collected results are:

```python
hero.moveXY(coin.pos.x, coin.pos.y)| hero.move(coin.pos)| moveToXY(coin.pos)
             55 gold | 63 gold | 65 gold

```

---

<div class="post-metadata">

**Author:** ![badandy1981](https://avatars.discourse-cdn.com/v4/letter/b/9d8465/32.png) [@badandy1981](https://discourse.codecombat.com/u/badandy1981)\
**Post date:** [March 9, 2020, 11:05pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/15 "2020-03-09T23:05:00Z")

</div>

The coins seem to have too much randomness in the time that they can despawn. Sometimes the coins stay long enough to collect two in the same group. Sometimes they seem to disappear before I can touch them.

---

<div class="post-metadata">

**Author:** ![Cedar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cedar/32/29302_2.png) [@Cedar](https://discourse.codecombat.com/u/Cedar)\
**Post date:** [June 18, 2022, 3:33pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/16 "2022-06-18T15:33:20Z")

</div>

My code also doesn’t work…

```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()
    while itemsIndex < len(items):
        item = items[itemsIndex]
        if item.value / hero.distanceTo(item) > bestValue:
            bestValue = item
            bestitem = 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.moveXY(coin.pos.x, coin.pos.y)

```

---

<div class="post-metadata">

**Author:** ![Cedar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cedar/32/29302_2.png) [@Cedar](https://discourse.codecombat.com/u/Cedar)\
**Post date:** [June 18, 2022, 3:35pm UTC](https://discourse.codecombat.com/t/solved-diamond-dozen-python-help/13295/17 "2022-06-18T15:35:02Z")

</div>

never mind, it works now.
