# Mixed unit tactics pyhton

**URL:** <https://discourse.codecombat.com/t/mixed-unit-tactics-pyhton/29574>\
**Category:** Level Help\
**Created:** [July 8, 2021, 7:31pm UTC](https://discourse.codecombat.com/t/mixed-unit-tactics-pyhton/29574 "2021-07-08T19:31:22Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [July 9, 2021, 11:58am UTC](https://discourse.codecombat.com/t/mixed-unit-tactics-pyhton/29574/2 "2021-07-09T11:58:05Z")

</div>

> [@Shlomo](#):
>
> ```python
> def commandTroops():
> friends = hero.findFriends()
> for friend in friends:  
> if friend.type != "palisade":
> for i in range(len(friends)):
> # Use % to wrap around defendPoints based on friendIndex
> # Command your minion to defend the defendPoint
> posi = defendPoints[i] % len(defendPoints)
> hero.command(friend, 'defend', defendPoints[posi])
> 
> ```

This function has a few problems.  
Firstly, why are you looping through your friends array twice? You do for friend in friends, then for i in range(len(friends)). You only want the for i in range(len(friends)). The first for loop is unnecessary.  
Secondly, posi is incorrectly defined (this is actually very difficult, so I understand you having difficulty with it). At the moment, you are doing defendPoints[i] % len(defendPoints). Remember that defendPoints[i] is actually a coordinate, like {“x”: 44, “y”: 55}. You can’t use the % operator on it. Instead, you want to use i by itself, and divide that by len(defendPoints).  
For more on % (modulo), you can read the post I wrote earlier, also about modulo, albeit in a different level:

> [@How Does the Length of Built Objects % Summon Types Work?](https://discourse.codecombat.com/t/how-does-the-length-of-built-objects-summon-types-work/29580/2):
>
> So, first of all you have the array of types, which each have an index (from 0, to however many items you have in the array - 1, because it started at 0, not 1) e.g. summonTypes = ["archer", "soldier", "peasant", "paladin"] 0 1 2 3 So len(hero.built)%len(summonTypes) will always return a result within 0-3. We know that len(summonTypes) will always equal 4, because the number of items in the array won’t change in this level. So that side of the oper…

Danny

---

_[View the full topic](https://discourse.codecombat.com/t/mixed-unit-tactics-pyhton/29574)._
