Steelclaw Gap help Python (is this level bugged?!)

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
point = defendPoints[len(friends) % len(defendPoints)]
# Command your minion to defend the defendPoint
hero.command(friend, “defend”, point)

while True:
summonTroops()
commandTroops()

What am I doing wrong?! I’ve had bugs on some levels, and there’s no clear reason why this code isn’t working! The only result I ever get is all the units are summoned and they all go to the top left corner! Please tell me there’s a bug in this level!

1 Like

Can you please format the code according to the FAQ please? So it will be easier for us to see the code. Thanks!

1 Like

I think the bug is in this line, I think it should be

point = defendPoints[friendIndex % len(defendPoints)]
1 Like

that doesn’t work either; I’d have to define friendIndex which would have to equal 0, because the friend variable is already defined as friends[i]. that’s why i think this level is bugged.

1 Like

@zbdude149

Please learn to post your code properly from the game from now on and use the </> button or it won’t format properly. When you click the </> button, the following will appear:

PostCode

There are many people here willing and able to help. If you use the </> button correctly, your code should look like this:

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
    else:
        hero.say("My code is formatted properly")

If your code doesn’t look like the code above, then you’re doing it wrong and we can’t see the structure of the code to troubleshoot whether or not that is the issue. Use the </> button and help us help you. This works when sending a private message as well. Thank you.

`

1 Like

Sorry, I didn’t see that you are using i for the index instead of friendIndex, because you’re using i, the code should be

point = defendPoints[i % len(defendPoints)]
2 Likes

thank you!!!
i got it!!