# [SOLVED] Steel claw gap

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

``````

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

``````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)
``````

so what should I put

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

thanks
(2000000000000000000 chars)

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.

# 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))’’’

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

oh, ok:

``````
# 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))``````

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

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

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

``````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))``````

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

1 Like

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

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…

1 Like