[SOLVED] Stuck cloudrip commander

while self.gold > self.costOf("soldier"):
    self.summon("soldier")
    
# Add a while loop to command all the soldiers.
soldiers = self.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
self.command(soldier, "move", {"x": 50, "y": 40})

# Go join your comrades!

help

# Add a while loop to command all the soldiers.

see comment in the default codeā€¦

soldiers = self.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]

will always be the same soldier on Position 1ā€¦ maybe change soldierIndex

yes. Those four lines after ā€œ# Add a whileā€¦ā€ donā€™t need to change, you just have to insert the ā€œwhile ā€¦:ā€ statement at the proper point (and indent the lines following).

2 Likes

i did it without using the while loop cause i didnt understand it is that ok

Well . . . . . . . . . That is and isnā€™t OK.

It is OK that you found a solution. :smile:
It isnā€™t OK that you didnā€™t learn how to use a while loop. :frowning:

I donā€™t think this is the first while-loop level. If it isnā€™t then go back to the previous level and reviewā€¦

If it is the first, then:

# Add a while loop to command all the soldiers.
soldiers = self.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
self.command(soldier, "move", {"x": 50, "y": 40})

As I mentioned in an earlier reply, you donā€™t ā€œchangeā€ any of those lines, you just ā€œAddā€ a while loop:

# Add a while loop to command all the soldiers.
soldiers = self.findFriends()
soldierIndex = 0
# pseudo code comments:
#while someIndexCounterThing some comparison to the length of some arrayOfObjects
    soldier = soldiers[soldierIndex]
    #someIndexCounterThing '-=' or '+=' 1
    self.command(soldier, "move", {"x": 50, "y": 40})
  • Replace the ā€œpseudo codeā€ while with a proper while for your language using:
  • the provided ā€œcounter/indexā€ (psst: soldierIndex)
  • the proper comparion < <= == => > (psst: the index is set to zero so it is less thanā€¦)
  • the length of the proper array (psst: you are commanding soldiers so the array isā€¦)
  • you also have to make sure that you increments/decrements the counter/index (psst: starting at zero means you are probably going to increment)
  • otherwise you have an infinite loop, that always does something with the ā€œfirstā€ soldierā€¦

When it comes to the comparison donā€™t forget that the length is the number of things in the array, but most (not all) computer languages count starting at zero not one, so the last item in the array is the ā€œlength-1ā€ item (0,1,2,3 = 4 items).

2 Likes

I am also stuck on this level and I have a question. What is supposed to replace #while someIndexCounterThing some comparison to the length of some arrayOfObjects ?

someIndexCounterThing is usually called index
some comparison is usually <, >, ==, <= or =>
length of some arrayOfObjects is usually array.length or len(array), depending on your language.

Put together this gives this:

#PYTHON
index = 0
while index < len(array):
    #do something with the array
    index++
//JAVASCRIPT
var index = 0;
while( index < array.length){
    //do something with the array
    index++;
}
1 Like

I get it now thanks!

Pls, here is my code i am stuck :frowning::

ā€˜ā€™ā€™# Summon some soldiers, then direct them to your base.

Each soldier costs 20 gold.

while self.gold > self.costOf(ā€œsoldierā€):
self.summon(ā€œsoldierā€)

soldiers = self.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
Solno = 0
while Solno < 5:# Add a while loop to command all the soldiers.
self.command(soldier, ā€œmoveā€, {ā€œxā€: 47, ā€œyā€: 40})
Solno += 1

Go join your comrades!

ā€˜ā€™ā€™

Thank you GeundericusTheMighty!

You have the right idea for code formatting, but the backtick is " ` ", not " ā€™ ". Itā€™s the one under the tilde on the upper left corner.

while self.gold > self.costOf("soldier"):
    self.summon("soldier")
    
soldiers = self.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
Solno = 0
while Solno < 5:# Add a while loop to command all the soldiers.
    self.command(soldier, "move", {"x": 47, "y": 40})
    Solno += 1
# Go join your comrades!
self.moveXY(50, 40)

Your problem is that you donā€™t redefine soldier. It stays as soldiers[soldierIndex] for the entire program. You should move that inside the loop.

Also, SolNo is unnecessary. You can just update soldierIndex.

Thank you

So this is what it should be?:


# Summon some soldiers, then direct them to your base.

# Each soldier costs 20 gold.
while self.gold > self.costOf("soldier"):
    self.summon("soldier")
    
soldiers = self.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
while soldierIndex  soldier:# Add a while loop to command all the soldiers.
    self.command(soldier, "move", {"x": 47, "y": 40})
    soldierIndex += 1
# Go join your comrades!
self.moveXY(50, 40)

Please donā€™t post correct code. That would make things too easy for others. Check whether the code works first. :wink:

In this case, though, you still have a problem: while soldierIndex soldier: doesnā€™t really mean anything. You had the right idea before, but with the wrong variable.

no need already.
Thanks. I solved it

Hey guys,
First of all. Thankā€™s for this awesome game. Iā€™m really enjoing it.

I have a question about line: soldier = soldiers[soldierIndex] . Why it has to be inside a loop? I took me about an hour to finish this task, because of it. Can someone explain it to me?

Hereā€™s an example:

friends = findFriends()
friendIndex = 0
friend = friends[friendIndex]
while friendIndex < len(friends):
    # Do stuff
    friendIndex += 1

friendIndex is forever defined as friends[0]. Therefore, you will never command the rest.