# Cloudrip Commander (Python)

**URL:** <https://discourse.codecombat.com/t/cloudrip-commander-python/27777>\
**Category:** Level Help\
**Created:** [March 5, 2021, 9:28pm UTC](https://discourse.codecombat.com/t/cloudrip-commander-python/27777 "2021-03-05T21:28:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Psyko2k](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/psyko2k/32/21180_2.png) [@Psyko2k](https://discourse.codecombat.com/u/Psyko2k)\
**Post date:** [March 5, 2021, 9:28pm UTC](https://discourse.codecombat.com/t/cloudrip-commander-python/27777/1 "2021-03-05T21:28:57Z")

</div>

hi guys,  
i’m really struggling with these loops. just when i think i’ve got them down i somehow create a infinite loop. could someone take a look at what i’ve done and explain what i’ve done wrong please.

the hero just stands there and only a few soldiers are summoned.

many thanks.

```python
# 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 soldierIndex < len(soldiers):
    soldier = soldiers[soldierIndex]
    hero.command(soldier, "move", {"x": 50, "y": 40})
soldierIndex += 1
# Go join your comrades!
hero.move({"x":50, "y" : 40})

```

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [March 5, 2021, 9:33pm UTC](https://discourse.codecombat.com/t/cloudrip-commander-python/27777/2 "2021-03-05T21:33:55Z")

</div>

> [@Psyko2k](#):
>
> ```python
> while hero.gold > hero.costOf("soldier"):
> hero.summon("soldier")
> 
> ```

Make that an if statement. You only want to summon if the gold is greater than or equal to the cost of the soldier.

> [@Psyko2k](#):
>
> `soldierIndex += 1`

Indent this, you want it to be in the while loop.

---

<div class="post-metadata">

**Author:** ![Psyko2k](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/psyko2k/32/21180_2.png) [@Psyko2k](https://discourse.codecombat.com/u/Psyko2k)\
**Post date:** [March 5, 2021, 9:51pm UTC](https://discourse.codecombat.com/t/cloudrip-commander-python/27777/3 "2021-03-05T21:51:13Z")

</div>

thanks 🙂

only problem is that if i change the while to an if he only summons one soldier.

with the followig code though, he summons all the soldiers, heads towards the X but it times out?

```python
# 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 soldierIndex < len(soldiers):
    soldier = soldiers[soldierIndex]
    hero.command(soldier, "move", {"x": 50, "y": 40})
    soldierIndex += 1
# Go join your comrades!
hero.move({"x":50, "y" : 40})

```

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/5/2/52c6f0c6e7f2c9f255d78cb4bdba73cc77613b4f.jpeg)

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [March 5, 2021, 9:53pm UTC](https://discourse.codecombat.com/t/cloudrip-commander-python/27777/4 "2021-03-05T21:53:27Z")

</div>

Why don’t you try moving with them, in the while loop, or move before them.

---

<div class="post-metadata">

**Author:** ![Psyko2k](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/psyko2k/32/21180_2.png) [@Psyko2k](https://discourse.codecombat.com/u/Psyko2k)\
**Post date:** [March 5, 2021, 9:58pm UTC](https://discourse.codecombat.com/t/cloudrip-commander-python/27777/5 "2021-03-05T21:58:20Z")

</div>

i tried moving my hero i the loop and also before.  
her just takes a couple of steps and stops

---

<div class="post-metadata">

**Author:** ![Psyko2k](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/psyko2k/32/21180_2.png) [@Psyko2k](https://discourse.codecombat.com/u/Psyko2k)\
**Post date:** [March 5, 2021, 10:27pm UTC](https://discourse.codecombat.com/t/cloudrip-commander-python/27777/6 "2021-03-05T22:27:26Z")

</div>

okay so i solved it (technically)

i had to use `hero.moveXY` instead of a dictionary.

i dont know why the dictionary didnt work.  
anyone have any ideas?

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [March 5, 2021, 10:31pm UTC](https://discourse.codecombat.com/t/cloudrip-commander-python/27777/7 "2021-03-05T22:31:28Z")

</div>

I didn’t notice you had move. Move will make your hero go to the spot, but only for a short period, If you have in in a loop, it will run as long as the loop is true. Not the case with moveXy, it makes you move to the spot at once.

---

<div class="post-metadata">

**Author:** ![Psyko2k](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/psyko2k/32/21180_2.png) [@Psyko2k](https://discourse.codecombat.com/u/Psyko2k)\
**Post date:** [March 5, 2021, 10:36pm UTC](https://discourse.codecombat.com/t/cloudrip-commander-python/27777/8 "2021-03-05T22:36:01Z")

</div>

okay i can see that, but when i moved it outside of the loop it still only took a few steps and stopped.

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [March 6, 2021, 8:44am UTC](https://discourse.codecombat.com/t/cloudrip-commander-python/27777/9 "2021-03-06T08:44:25Z")

</div>

> [@Psyko2k](#):
>
> but when i moved it outside of the loop it still only took a few steps and stopped.

Sorry if I’m causing more confusion, but I think that’s because you used hero.move(). If you use hero.move() outside a loop it doesn’t work.  
Even if you put it inside a loop it doesn’t always work.  
It didn’t work when you put it in second loop because the condition was only true for a short amount of time (soldierIndex \< len(soldiers). You can command soldiers once and they keep moving to wherever you commanded them to go until you tell them to stop. With hero.move() however, you only moved while you were commanding the soldiers to move (which doesn’t take very long), so you only moved half the distance that the soldiers did.  
To solve this you could use hero.moveXY() as you have done, or you could do something like:  
while hero.pos.x \< 50:  
hero.move()…  
Outside the second loop.  
Danny

---

<div class="post-metadata">

**Author:** ![Psyko2k](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/psyko2k/32/21180_2.png) [@Psyko2k](https://discourse.codecombat.com/u/Psyko2k)\
**Post date:** [March 6, 2021, 10:47am UTC](https://discourse.codecombat.com/t/cloudrip-commander-python/27777/10 "2021-03-06T10:47:10Z")

</div>

thanks @Deadpool198 ,  
I get it now, makes complete sense.  
And yes, i think that using `while hero.pos.x < 50 #or whatever number it is` would have been the best idea.

Thanks 😃

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [March 6, 2021, 10:48pm UTC](https://discourse.codecombat.com/t/cloudrip-commander-python/27777/11 "2021-03-06T22:48:01Z")

</div>

Closed on March 6, 2021, 10:48pm UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
