#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 thewhile
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")
Help! I,m stuck on Cloudrip Commander.
Jungly, we canât help you unless you talk about a specific case
Example:
- I donât know how to do this_simple_step
- I am trying to do this_simple_step but instead I get this_error on this_line.
- 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 besoldierIndex < 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():
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.
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)
Please format your code according to the FAQ using the triple backticks or this button:
</>
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.
No problem! And congratulations for completing the level!