# \[SOLVED\] Steel claw gap

**URL:** <https://discourse.codecombat.com/t/solved-steel-claw-gap/14307>\
**Category:** Level Help\
**Created:** [March 30, 2018, 2:41am UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307 "2018-03-30T02:41:30Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon25065908](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@anon25065908](https://discourse.codecombat.com/u/anon25065908)\
**Post date:** [March 30, 2018, 2:41am UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/1 "2018-03-30T02:41:30Z")

</div>

```python
# This level introduces the % operator, also known as the modulo operator.
# a % b returns the remainder of a divided by b
# This can be used to wrap around to the beginning of an array when an index might be greater than the length

defendPoints = [{"x": 35, "y": 63},{"x": 61, "y": 63},{"x": 32, "y": 26},{"x": 64, "y": 26}]

summonTypes = ["soldier","soldier","soldier","soldier","archer","archer","archer","archer"]

# You start with 360 gold to build a mixture of soldiers and archers.
# self.built is an array of the troops you have built, ever.
# Here we use "len(self.built) % len(summonTypes)" to wrap around the summonTypes array
def summonTroops():
    type = summonTypes[len(hero.built) % len(summonTypes)]
    if hero.gold >= hero.costOf(type):
        hero.summon(type)

def commandTroops():
    friends = hero.findFriends()
    for i in range(len(friends)):
        friend = friends[i]
        # Use % to wrap around defendPoints based on friendIndex
        defendPoints = defendPoints[friendIndex % len(defendPoints)]
        # Command your minion to defend the defendPoint
        hero.command(friend, "defend", defendPoints)

while True:
    summonTroops()
    commandTroops()

```

---

<div class="post-metadata">

**Author:** ![Gabriel\_Dupuis](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gabriel_dupuis/32/7450_2.png) [@Gabriel\_Dupuis](https://discourse.codecombat.com/u/Gabriel_Dupuis)\
**Post date:** [March 30, 2018, 2:46am UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/2 "2018-03-30T02:46:51Z")

</div>

You are redefining what is defendPoints in your loop, So the result is all your soldier all go on the same defendpoint.

```python
def commandTroops():
    friends = hero.findFriends()
    for i in range(len(friends)):
        friend = friends[i]
        # Use % to wrap around defendPoints based on friendIndex
        defendPoints = defendPoints[friendIndex % len(defendPoints)] # <--- problem is here 
        # Command your minion to defend the defendPoint
        hero.command(friend, "defend", defendPoints)

```

---

<div class="post-metadata">

**Author:** ![anon25065908](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@anon25065908](https://discourse.codecombat.com/u/anon25065908)\
**Post date:** [March 31, 2018, 2:17pm UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/3 "2018-03-31T14:17:49Z")

</div>

so what should I put

---

<div class="post-metadata">

**Author:** ![Gabriel\_Dupuis](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gabriel_dupuis/32/7450_2.png) [@Gabriel\_Dupuis](https://discourse.codecombat.com/u/Gabriel_Dupuis)\
**Post date:** [March 31, 2018, 8:33pm UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/4 "2018-03-31T20:33:00Z")

</div>

I’ll try to make you understand whats going on you’ll figure out what to do after

let’s say your amounts of defendpoints is 4 and are using those values

**defendPoints** = defendPoints[**friendIndex** % len(defendPoints)]

First thing **friendIndex is not defined**  
the value that increment in a for loop is the value **i** you used.

so right now your code say: defendPoints = defendPoints[**Null** % len(defendPoints)]

But there is another problem let’s say you have the correct value i in that code

the first loop i = 0

The computer will do this:

defendPoints = defendPoints[**i** % len(defendPoints)] **(the len value is 4)**

_ **if we are in the first loop, i = 0** _

defendPoints = defendPoints[0 % len(defendPoints)] **0 % 4 = 0**  
defendPoints = defendPoints[**0**]

here what happen when you do that:  
defendPoints = {“x”: 35, “y”: 63}  
**defendPoints is redefined to {“x”: 35, “y” 63} which is defendPoints[0]**

**(the len value of defendPoints is now 1)**

lets go for the second loop now, i = 1  
defendPoints = defendPoints[i % len(defendPoints)] **1 % 1 is still 0**  
defendPoints = defendPoints[0]  
defendPoints = {“x”: 35, “y” 63}

so basically you are losing your data in the variable defendPoints  
and friendIndex is not defined

Im sorry if I am not giving you the answer straight ahead I believe if you manage to understand you’ll have a better knowledge

---

<div class="post-metadata">

**Author:** ![anon25065908](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@anon25065908](https://discourse.codecombat.com/u/anon25065908)\
**Post date:** [April 4, 2018, 2:12pm UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/5 "2018-04-04T14:12:32Z")

</div>

thanks  
(2000000000000000000 chars)

---

<div class="post-metadata">

**Author:** ![Ian\_Spurgeon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ian_spurgeon/32/21987_2.png) [@Ian\_Spurgeon](https://discourse.codecombat.com/u/Ian_Spurgeon)\
**Post date:** [April 16, 2021, 9:08pm UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/6 "2021-04-16T21:08:50Z")

</div>

Help! It keeps saying “elif must be paired with if,” but, it _is_:  
Here is my code  
‘’'# You start with 360 gold to build a mixture of soldiers and archers.

# self.built is an array of the troops you have built, ever.

# Here we use “len(self.built) % len(summonTypes)” to wrap around the summonTypes array

def summonTroops():  
type = summonTypes[len(hero.built) % len(summonTypes)]  
if hero.gold \>= hero.costOf(type):  
hero.summon(type)

def commandSoldier(soldier, soldierIndex, numSoldiers):  
defendPos1 = ({“x”: 48,“y”: 44})  
if(soldier.health \> 60):  
hero.command(soldier, “defend”, defendPos1)  
elif soldier[8] or soldier[9] or soldier[10] or soldier[11] :  
hero.command(soldier, “defend”, {‘x’:66,‘y’:63}  
elif soldier[13] or soldier[14] or soldier[15] or soldier[12] :  
hero.command(soldier, “defend”, {‘x’: 66, ‘y’: 27})  
else:  
hero.command(soldier, “defend”, target)

while True:  
summonTroops()  
soldiers = hero.findByType(“soldier”)  
for i in range(len(soldiers)):  
soldier = soldiers[i]  
commandSoldier(soldier, i, len(soldiers))‘’’

---

<div class="post-metadata">

**Author:** ![RenegadePenguin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/renegadepenguin/32/21959_2.png) [@RenegadePenguin](https://discourse.codecombat.com/u/RenegadePenguin)\
**Post date:** [April 16, 2021, 10:54pm UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/7 "2021-04-16T22:54:03Z")

</div>

@Ian_Spurgeon could you format your code so that it’s more readable?

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [April 16, 2021, 11:06pm UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/8 "2021-04-16T23:06:57Z")

</div>

Format it with the button that looks like this: \</\>.

---

<div class="post-metadata">

**Author:** ![Ian\_Spurgeon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ian_spurgeon/32/21987_2.png) [@Ian\_Spurgeon](https://discourse.codecombat.com/u/Ian_Spurgeon)\
**Post date:** [April 24, 2021, 1:57am UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/9 "2021-04-24T01:57:50Z")

</div>

oh, ok:

```python

# self.built is an array of the troops you have built, ever.

# Here we use “len(self.built) % len(summonTypes)” to wrap around the summonTypes array

def summonTroops():
type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold >= hero.costOf(type):
hero.summon(type)

def commandSoldier(soldier, soldierIndex, numSoldiers):
defendPos1 = ({“x”: 48,“y”: 44})
if(soldier.health > 60):
hero.command(soldier, “defend”, defendPos1)
elif soldier[8] or soldier[9] or soldier[10] or soldier[11] :
hero.command(soldier, “defend”, {‘x’:66,‘y’:63}
elif soldier[13] or soldier[14] or soldier[15] or soldier[12] :
hero.command(soldier, “defend”, {‘x’: 66, ‘y’: 27})
else:
hero.command(soldier, “defend”, target)

while True:
summonTroops()
soldiers = hero.findByType(“soldier”)
for i in range(len(soldiers)):
soldier = soldiers[i]
commandSoldier(soldier, i, len(soldiers))
```

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [April 24, 2021, 5:10am UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/10 "2021-04-24T05:10:47Z")

</div>

I’m not at summoning yet so you’ll want someone better in coding like @RenegadePenguin or @thebagel, and others.

---

<div class="post-metadata">

**Author:** ![RenegadePenguin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/renegadepenguin/32/21959_2.png) [@RenegadePenguin](https://discourse.codecombat.com/u/RenegadePenguin)\
**Post date:** [April 24, 2021, 3:26pm UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/11 "2021-04-24T15:26:59Z")

</div>

Is that exactly what your code looks like or does it have indents?

---

<div class="post-metadata">

**Author:** ![Ian\_Spurgeon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ian_spurgeon/32/21987_2.png) [@Ian\_Spurgeon](https://discourse.codecombat.com/u/Ian_Spurgeon)\
**Post date:** [April 24, 2021, 6:16pm UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/12 "2021-04-24T18:16:36Z")

</div>

@RenegadePenguin Indents, in all the proper places. More like this:

```python
type = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold >= hero.costOf(type):
    hero.summon(type)

def commandSoldier(soldier, soldierIndex, numSoldiers):
    defendPos1 = ({“x”: 48,“y”: 44})
    if(soldier.health > 60):
        hero.command(soldier, “defend”, defendPos1)
    elif soldier[8] or soldier[9] or soldier[10] or soldier[11] :
        hero.command(soldier, “defend”, {‘x’:66,‘y’:63}
    elif soldier[13] or soldier[14] or soldier[15] or soldier[12] :
        hero.command(soldier, “defend”, {‘x’: 66, ‘y’: 27})
    else:
        hero.command(soldier, “defend”, target)

while True:
    summonTroops()
    soldiers = hero.findByType(“soldier”)
    for i in range(len(soldiers)):
        soldier = soldiers[i]
        commandSoldier(soldier, i, len(soldiers))
```

---

<div class="post-metadata">

**Author:** ![Ian\_Spurgeon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ian_spurgeon/32/21987_2.png) [@Ian\_Spurgeon](https://discourse.codecombat.com/u/Ian_Spurgeon)\
**Post date:** [April 24, 2021, 6:18pm UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/13 "2021-04-24T18:18:06Z")

</div>

@enPointe77 Ok. You’ll get there shortly after you get into cloudrip mountain. 😃

---

<div class="post-metadata">

**Author:** ![thebagel](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/thebagel/32/22241_2.png) [@thebagel](https://discourse.codecombat.com/u/thebagel)\
**Post date:** [April 24, 2021, 6:43pm UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/14 "2021-04-24T18:43:52Z")

</div>

Change those quotation marks to quotation marks. in your code.

> [@Ian\_Spurgeon](#):
>
> ```python
> hero.command(soldier, “defend”, target)
> 
> ```

where is target defined

---

<div class="post-metadata">

**Author:** ![Ian\_Spurgeon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ian_spurgeon/32/21987_2.png) [@Ian\_Spurgeon](https://discourse.codecombat.com/u/Ian_Spurgeon)\
**Post date:** [April 24, 2021, 8:37pm UTC](https://discourse.codecombat.com/t/solved-steel-claw-gap/14307/15 "2021-04-24T20:37:25Z")

</div>

> [@thebagel](#):
>
> where is target defined

Oh, oops must have missed that with it throwing all the other errors. Thanks, by the way.

Later, I had found an alternate solution, for while I did not know how to do it the right way, if you could explain how you would use % for this level, it would be appreciated, as i could also have a chance at getting the bonus…
