Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Computer science 5: level 22 to arms (python)

here is my code, it works but i cannot complete the level because it always runs out of time and i am not sure whats wrong with it.

# Ogres are going to attack soon.
# Move near each of tents (to the X marks)
# say() something at each X to wake your soldiers.
# Beware: leave the camp when the battle begins!
# Ogres will send reinforcements if they see the hero.
# The sergeant knows the distance between tents.
sergeant =  hero.findNearest(hero.findFriends())
# The distances between the X marks.
stepX = sergeant.tentDistanceX
stepY = sergeant.tentDistanceY
# The number of tents.
tentsInRow = 5
tentsInColumn = 4
# The first tent mark has constant coordinates.
firstX = 10
firstY = 14

while True :
    for x in range(firstX, 61, stepX) :
        for y in range(firstY, 78, stepY) :
            hero.moveXY(x, y)
            hero.say("yay")
hero.moveXY(6,9)


The issue is that you go up-down when you need to go left-right. So put instead of this:

This:

for y in range(firstY, firstY+stepY*4+1, stepY):
    for x in range(firstX, firstX+stepX*5+1, stepX):

I see one definite issue and one possible. For the definite, your x and y for loops are transposed and missing some calculations.

For the possible, you don’t need the ‘while True’ statement, as the two for loops will complete the code appropriately…once they are resolved.

I’ll leave it to @AnSeDra from here…he was the first to respond :wink:

1 Like

I put that things instead of numbers to be more accurate.

If you will keep the while True loop, then put 4 spaces before the moveXY.
Does it work now?

1 Like

It still doesn’t work for some reason, and I tried putting for y in range(firstY, firstY+stepY*4+1, stepY): for x in range(firstX, firstX+stepX*5+1, stepX): too but it didn’t work for some reason.

The order is first y, then x. Do you get the out of time thing?

1 Like

Can you send me the code that you now have?

I actually fixed it now, thank you so much for your help! I just followed your instructions wrong woops haha.

Congratulations! :partying_face:

1 Like

can you edit the title by putting [SOLVED] at the beginning please?

1 Like

Oh sure. Thanks for reminding me.

1 Like

Done. :smiley: :slightly_smiling_face:

1 Like