Stuck cloudrip commander


#1
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


#2
# Add a while loop to command all the soldiers.

see comment in the default code…


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

will always be the same soldier on Position 1… maybe change soldierIndex


#4

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).


#5


#6

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


#7

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).


#10

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 ?


#11

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++;
}

#12

I get it now thanks!


#13

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!

‘’’


#14

Thank you GeundericusTheMighty!


#15

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.


#16
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)


#17

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.


#18

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)


#19

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.


#21

no need already.
Thanks. I solved it


#22

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?


#23

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.