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?