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!
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).
It is OK that you found a solution.
It isn’t OK that you didn’t learn how to use a while loop.
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).
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++;
}
# 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.
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.
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?