[SOLVED] Stuck cloudrip commander

#Help! my code is not working

while self.gold > self.costOf("soldier"):
    self.summon("soldier")
hi = 0    
while hi < 6:
    soldiers = self.findFriends()
    soldierIndex = 0
    soldier = soldiers[soldierIndex]
    self.command(soldier, "move", {"x": 50, "y": 40})
    hi += 1
    self.moveXY(50, 40)
    solderIndex += 1

Hello, Hanoo, and welcome. Please read the FAQ before you post again. It is essential that you learn how to format your code properly. I have done it for you this time, but please do it yourself in the future.

We need more information than “This code doesn’t work!” or any such variants. Give us a detailed description of you error.

Your basic idea is OK, but you reset the index every time the loop runs with the soldierIndex = 0 line. Move that outside the loop and it should be OK.


Further points to improve:

  • you can use soldierIndex for the while loop condition
  • so you don’t really need the hi variable
  • you can safely move the soldiers = self.findFriends() line out of the loop
  • you don’t need to move to the destination every time the loop only when you finished sending your friends there – move that out of the loop too (hint: unindent)

Cheers

This level might need to be fixed so people can’t use simple commands like this

self.moveXY(50, 42)
loop:
    self.summon("soldier")
1 Like

Help! I,m stuck on Cloudrip Commander.

Jungly, we can’t help you unless you talk about a specific case
Example:

  1. I don’t know how to do this_simple_step
  2. I am trying to do this_simple_step but instead I get this_error on this_line.
  3. I am trying to do this_devious_plan but when the code runs this_totally_dispointing_thing happens
    I am attacking my code formated by surrounding with 3 backslashes ``` (top-left symbol on the keyboard)

My code here

Help! I need help on cloudrip commander!

My code:

# 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]
# Add a while loop to command all the soldiers.
while soldierIndex soldier:
    soldier = soldiers[soldierIndex]
    self.command(soldier, "move", {"x": 50, "y": 40})

# Go join your comrades!

Please format your code correctly. I’ve formatted it for you this time.

Your while loop has quite a few problems.

  • The condition while soldierIndex soldier is wrong. It should be soldierIndex < len(soldiers)
  • soldier = soldiers[soldierIndex] should be inside the loop
  • You should increment the soldierIndex variable at the end of the loop.

Please review how you’ve done these loops in previous levels.
Or just use a for soldier in self.findFriends():

1 Like

thanks for the info.I will use it for other levels. By the way, I really do need help on the level treasure cave.
Can you help?

My Code:

That ogre died instantly! Better just avoid that yeti.

enemy = self.findEnemies()
distance = self.distanceTo(enemy)
item = self.findItems()

We’ve hacked your fire-traps to go off after 5 seconds.

The clearing to the north would be a good place for an explosive distraction.

The yeti won’t stay distracted forever, so hurry and grab the coins!

loop:
self.move({“x”: 73, “y”: 40})
if self.distanceTo(enemy) < 10:
self.move({“x”: 72, “y”: 9})

Obviously you did not listen to UltCombo’s advice. Although yes, us moderators can format your code for you, we can’t be mother-henning you every hour of the day. Please read the FAQ, accessed by UltCombo’s link, so then we can help you.

1 Like

I have tried to rebeat this level, and have been having difficulties with the while loop.

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

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

# Go join your comrades!
hero.move({"x": 50, "y": 40})

When I run the program, all it does is command one (of four summoned) soldiers and proceed to run out of time.
Any advice?

The line:

soldier = soldiers[soldierIndex]

Then you command the soldier, but don’t actually increase the index. Remember back to the Desert levels about iterating over an array using a while loop.

Help! My character just stops when all the soldiers reach the base!

Summon some soldiers, then direct them to your base.

Each soldier costs 20 gold.

while hero.gold > hero.costOf(“soldier”):
hero.summon(“soldier”)

soldiers = hero.findFriends()
soldierIndex = 0

Add a while loop to command all the soldiers.

while soldiers:
soldier = soldiers[soldierIndex]
hero.command(soldier, “move”, {“x”: 50, “y”: 40})
soldierIndex += 1

Go join your comrades!

hero.moveXY(50, 40)

1 Like

Please format your code according to the FAQ using the triple backticks or this button:

</>

:slightly_smiling_face:

Use while soldierIndex < len(soldiers):
Don’t use while soldiers

Also you should use if hero.gold... not while hero.gold... because when using the while loop the hero only does this while hero.gold is greater than or equal to the cost of a soldier and then breaks the loop.

Your while loop “while True” will always run and never stop so you’ll never get to the code after your while loop.

Also you’re not changing the value of soldierIndex so you’re commanding the same soldier each time.

soldiers is a list of soldiers soldiers[0], soldiers[1], soldiers[2]…soldiers[len(soldiers)-1]

So inside your while loop you want to change soldierIndex so that you’re commanding a different soldier each time through. Then you want your evaluation for the while loop to be something that lets the loop exit after all of the soldiers have been commanded instead of always being True forever. There are a couple of examples above you can piece together to figure this out.

I need help this is my code

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

This one put after

So the soldier will keep updating. Also put 4 spaces before

Do you need any more assistance at this level?

no thanks for helping me.

1 Like

No problem! And congratulations for completing the level! :partying_face: