# Help, Library Tactician! \[PYTHON\]

**URL:** <https://discourse.codecombat.com/t/help-library-tactician-python/19369>\
**Category:** Level Help\
**Created:** [October 10, 2019, 3:26pm UTC](https://discourse.codecombat.com/t/help-library-tactician-python/19369 "2019-10-10T15:26:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![CranKeD\_Wired](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cranked_wired/32/10796_2.png) [@CranKeD\_Wired](https://discourse.codecombat.com/u/CranKeD_Wired)\
**Post date:** [October 10, 2019, 3:26pm UTC](https://discourse.codecombat.com/t/help-library-tactician-python/19369/1 "2019-10-10T15:26:32Z")

</div>

Hello, I have been working on this level for a while now but can’t seem to keep all of the soldiers alive, the same one always dies, does anyone have any suggestions? ☹

```python
def commandSoldier(soldier, soldierIndex, numSoldiers):
    angle = Math.PI * 2 * soldierIndex / numSoldiers
    defendPos = {"x": 41, "y": 40}
    defendPos.x += 10 * Math.cos(angle)
    defendPos.y += 10 * Math.sin(angle)
    self.command(soldier, "defend", defendPos);
pass

def findStrongestTarget():
    mostHealth = 0
    enemies = self.findEnemies()
    enemyIndex = 0
    while len(enemies) > enemyIndex:
        enemy = enemies[enemyIndex]
        if enemy:
            enemyIndex += 1
            if enemy.health > mostHealth:
                mostHealth = enemy.health
                bestTarget = enemy
    if bestTarget and bestTarget.health > 15:
        return bestTarget
    else:
        return None
    pass

def commandArcher(archer):
    nearest = archer.findNearestEnemy()
    if archerTarget:
        self.command(archer, "attack", archerTarget)
    elif nearest:
        self.command(archer, "attack", nearest)
        

archerTarget = None

while True:
    if self.gold > self.costOf("soldier"):
        self.summon("soldier")
    if not archerTarget or archerTarget.health <= 0:
        archerTarget = findStrongestTarget()
        friends = self.findFriends()
    soldiers = self.findByType("soldier")
    archers = self.findByType("archers")
    for i, soldier in enumerate(soldiers):
        commandSoldier(soldier, i, len(soldiers));
        for i in range(len(archers)):
            archer = archers[i]
            commandArcher(archer)
```

---

<div class="post-metadata">

**Author:** ![Yuriko](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/yuriko/32/11530_2.png) [@Yuriko](https://discourse.codecombat.com/u/Yuriko)\
**Post date:** [March 6, 2020, 9:12pm UTC](https://discourse.codecombat.com/t/help-library-tactician-python/19369/2 "2020-03-06T21:12:07Z")

</div>

Adding this line to the “commandSoldier()” should work:

```python
def commandSoldier(soldier, soldierIndex, numSoldiers):
    angle = Math.PI * 2 * soldierIndex / numSoldiers
    defendPos = {"x": 41, "y": 40}
    if soldier.health > soldier.maxHealth * 0.3:
        defendPos.x += 10 * Math.cos(angle)
        defendPos.y += 10 * Math.sin(angle)
    self.command(soldier, "defend", defendPos);

```

---

<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:** [July 17, 2020, 2:34pm UTC](https://discourse.codecombat.com/t/help-library-tactician-python/19369/5 "2020-07-17T14:34:07Z")

</div>

Please do not revive dead topics unless you have a similar issue. @CranKeD_Wired hasn’t been on the forum for almost a year, so there is a pretty good chance that he will not even see your question.

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:** [October 18, 2020, 9:44am UTC](https://discourse.codecombat.com/t/help-library-tactician-python/19369/7 "2020-10-18T09:44:19Z")

</div>

hello @AnSeDra is there a mistake with my code?

```python
def commandSoldier(soldier, soldierIndex, numSoldiers):
    angle = Math.PI * 2 * soldierIndex / numSoldiers
    defendPos = {"x": 41, "y": 40}
    defendPos.x += 10 * Math.cos(angle)
    defendPos.y += 10 * Math.sin(angle)
    self.command(soldier, "defend", defendPos);
pass

def findStrongestTarget():
    mostHealth = 0
    enemies = hero.findEnemies()
    enemyIndex = 0
    while len(enemies) > enemyIndex:
        enemy = enemies[enemyIndex]
        if enemy:
            enemyIndex += 1
            if enemy.health > mostHealth:
                mostHealth = enemy.health
                bestTarget = enemy
    if bestTarget and bestTarget.health > 15:
        return bestTarget
    else:
        return None
    pass

def commandSoldier(soldier, soldierIndex, numSoldiers):
    angle = Math.PI * 2 * soldierIndex / numSoldiers
    defendPos = {"x": 41, "y": 40}
    if soldier.health > soldier.maxHealth * 0.3:
        defendPos.x += 10 * Math.cos(angle)
        defendPos.y += 10 * Math.sin(angle)
    self.command(soldier, "defend", defendPos)

archerTarget = None

while True:
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
    if not archerTarget or archerTarget.health <= 0:
        archerTarget = findStrongestTarget()
        friends = self.findFriends()
    soldiers = hero.findByType("soldier")
    archers = hero.findByType("archers")
    for i, soldier in enumerate(soldiers):
        commandSoldier(soldier, i, len(soldiers));
        for i in range(len(archers)):
            archer = archers[i]
            commandArcher(archer)
```

---

<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:** [October 18, 2020, 9:46am UTC](https://discourse.codecombat.com/t/help-library-tactician-python/19369/8 "2020-10-18T09:46:34Z")

</div>

and why do you always put andrei in the end of your sentence

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [October 19, 2020, 12:55am UTC](https://discourse.codecombat.com/t/help-library-tactician-python/19369/9 "2020-10-19T00:55:05Z")

</div>

Hello @wazi,

@AnSeDra is currently suspended so, he/she will not see your message. AnSeDra puts Andrei at the end of his sentence because (I’m assuming) that is his/her name.  
Lydia

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [October 19, 2020, 1:01am UTC](https://discourse.codecombat.com/t/help-library-tactician-python/19369/10 "2020-10-19T01:01:12Z")

</div>

> [@wazi](#):
>
> ```python
> def commandSoldier(soldier, soldierIndex, numSoldiers):
> angle = Math.PI * 2 * soldierIndex / numSoldiers
> defendPos = {"x": 41, "y": 40}
> if soldier.health > soldier.maxHealth * 0.3:
> defendPos.x += 10 * Math.cos(angle)
> defendPos.y += 10 * Math.sin(angle)
> self.command(soldier, "defend", defendPos)
> 
> ```

Why do you need this twice?

> [@wazi](#):
>
> `pass`

You do not need `pass` at the end of every def

> [@wazi](#):
>
> `self`

I personally think hero is better.

> [@wazi](#):
>
> ```python
> enemyIndex = 0
> while len(enemies) > enemyIndex:
> enemy = enemies[enemyIndex]
> if enemy:
> enemyIndex += 1
> if enemy.health > mostHealth:
> mostHealth = enemy.health
> bestTarget = enemy
> 
> ```

I also think you do not need to go this much trouble. You can use a simple for loop.

> [@wazi](#):
>
> ```python
> def commandSoldier(soldier, soldierIndex, numSoldiers):
> angle = Math.PI * 2 * soldierIndex / numSoldiers
> defendPos = {"x": 41, "y": 40}
> if soldier.health > soldier.maxHealth * 0.3:
> defendPos.x += 10 * Math.cos(angle)
> defendPos.y += 10 * Math.sin(angle)
> self.command(soldier, "defend", defendPos)
> 
> ```

This should be

```python
def commandArcher(archer):

```

> [@wazi](#):
>
> ```python
> if hero.gold > hero.costOf("soldier"):
> hero.summon("soldier")
> 
> ```

You don’t need to summon more soldiers

> [@wazi](#):
>
> ` friends = self.findFriends()`

You need to delete an indent.

> [@wazi](#):
>
> ```python
> for i, soldier in enumerate(soldiers):
> commandSoldier(soldier, i, len(soldiers));
> 
> ```

You can just say:

```python
for i in range(len(soldiers)):
        soldier = soldiers[i]
        commandSoldier(soldier, i, len(soldiers))

```

> [@wazi](#):
>
> ```python
> for i in range(len(archers)):
> archer = archers[i]
> commandArcher(archer)
> 
> ```

This needs to be deleted an indent.  
And you can just use:

```python
for archer in archers:

```

I also recommend sticking with either `self` or `hero` as I see that you use both.  
Lydia

---

<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:** [October 21, 2020, 10:16am UTC](https://discourse.codecombat.com/t/help-library-tactician-python/19369/11 "2020-10-21T10:16:54Z")

</div>

Thank you very much for all the helps!

---

<div class="post-metadata">

**Author:** ![098765432123](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/098765432123/32/25850_2.png) [@098765432123](https://discourse.codecombat.com/u/098765432123)\
**Post date:** [October 21, 2020, 2:31pm UTC](https://discourse.codecombat.com/t/help-library-tactician-python/19369/12 "2020-10-21T14:31:46Z")

</div>

so you passed the level? congrats! 🥳

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [October 21, 2020, 2:35pm UTC](https://discourse.codecombat.com/t/help-library-tactician-python/19369/14 "2020-10-21T14:35:14Z")

</div>

Congrats on solving the level!  
Lydia
