# \[SOLVED\]Problem with function

**URL:** https://discourse.codecombat.com/t/solved-problem-with-function/11099
**Category:** Uncategorized
**Created:** [April 5, 2017, 6:15pm UTC](https://discourse.codecombat.com/t/solved-problem-with-function/11099 "2017-04-05T18:15:37Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Szymon859](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@Szymon859](https://discourse.codecombat.com/u/Szymon859)
#### Post date: [April 5, 2017, 6:15pm UTC](https://discourse.codecombat.com/t/solved-problem-with-function/11099/1 "2017-04-05T18:15:38Z")

</div>

When i run my code it shows this error.

```python
def findWeakestEnemy():
    enemies = hero.findEnemies()
    weakest = None
    leastHealth = 99999
    enemyIndex = 0
    # Loop through enemies:
    while enemyIndex < len(enemies):
        # If an enemy's health is less than leastHealth:
        enemy = enemies[enemyIndex]
        if enemy.health < leastHealth:
            # Make it the weakest 
            enemy[enemyIndex] = weakest
            # and set leastHealth to its health.
            leastHealth = enemy[enemyIndex].health
            pass
        enemyIndex += 1
        pass
    return weakest

while True:
    # Find the weakest enemy with the function:
    findWeakestEnemy()
    # If the weakest enemy here:
    if weakest:
        # Attack it!
        hero.attack(weakest)
    pass

```

 ![](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/8/8f285cc013604733302885b7639b95be1cf0b87d.PNG)

---

<div class="post-metadata">

### Author: ![Hellenar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hellenar/32/9100_2.png) [@Hellenar](https://discourse.codecombat.com/u/Hellenar)
#### Post date: [April 5, 2017, 6:43pm UTC](https://discourse.codecombat.com/t/solved-problem-with-function/11099/2 "2017-04-05T18:43:32Z")

</div>

You do not need to put `enemy[enemyIndex]` when you redefine `weakest`, just `enemy`. Same deal with the lowest health.  
Also it is `weakest = enemy`, not `enemy = weakest`.  
Happy to help 🙂

---

<div class="post-metadata">

### Author: ![\_TD\_RodYT](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/_td_rodyt/32/6285_2.png) [@\_TD\_RodYT](https://discourse.codecombat.com/u/_TD_RodYT)
#### Post date: [April 5, 2017, 6:45pm UTC](https://discourse.codecombat.com/t/solved-problem-with-function/11099/3 "2017-04-05T18:45:12Z")

</div>

Make sure you assign findWeakestEnemy to a variable; For example:

weakestEnemy = findWeakestEnemy()

if weakestEnemy:

---

<div class="post-metadata">

### Author: ![Szymon859](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@Szymon859](https://discourse.codecombat.com/u/Szymon859)
#### Post date: [April 6, 2017, 2:50pm UTC](https://discourse.codecombat.com/t/solved-problem-with-function/11099/4 "2017-04-06T14:50:34Z")

</div>

Now i’m getting this

 ![](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/0/078cf547598648cf5c1ca7fe0a02f024cf855d1b.PNG)

---

<div class="post-metadata">

### Author: ![Hellenar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hellenar/32/9100_2.png) [@Hellenar](https://discourse.codecombat.com/u/Hellenar)
#### Post date: [April 6, 2017, 2:55pm UTC](https://discourse.codecombat.com/t/solved-problem-with-function/11099/5 "2017-04-06T14:55:35Z")

</div>

Try `leastHealth = enemy.health`

---

<div class="post-metadata">

### Author: ![Szymon859](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@Szymon859](https://discourse.codecombat.com/u/Szymon859)
#### Post date: [April 6, 2017, 2:57pm UTC](https://discourse.codecombat.com/t/solved-problem-with-function/11099/6 "2017-04-06T14:57:47Z")

</div>

Now it works! Thanks

---

<div class="post-metadata">

### Author: ![Hellenar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hellenar/32/9100_2.png) [@Hellenar](https://discourse.codecombat.com/u/Hellenar)
#### Post date: [April 6, 2017, 2:58pm UTC](https://discourse.codecombat.com/t/solved-problem-with-function/11099/7 "2017-04-06T14:58:37Z")

</div>

Happy to help, @Szymon859! 🙂

---

<div class="post-metadata">

### Author: ![\_TD\_RodYT](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/_td_rodyt/32/6285_2.png) [@\_TD\_RodYT](https://discourse.codecombat.com/u/_TD_RodYT)
#### Post date: [April 6, 2017, 3:24pm UTC](https://discourse.codecombat.com/t/solved-problem-with-function/11099/8 "2017-04-06T15:24:58Z")

</div>

Nice Job @Hellenar. I did not see that!
