I’m at the end of the first level (Kithgard’s Dungeon) but I’m still having trouble with the While True Loop. Whenever I use one to have a cleaner code, whatever I write after it (without the 4 spaces in front of it) won’t play!
So what is the code to make it stop when let’s say the enemy is defeated? Is there a code for this? I’d really love to implement this use, but I can only use it once and at the end in order for everything to work! It’s bothersome.
For example, this code wouldn’t work:
while True:
enemy1 = hero.findNearestEnemy()
if enemy1:
hero.attack(enemy1)
hero.moveRight()
The character would just stand there, idling.
I hope you can help me. I’m new to coding but I already love it!
Hi @Catopatra,
I think what you want is break, it only comes in later levels (mountain/glacier) but I’m almost 100% sure you can use it without the equivalent Promatticon.
A possible use (as in your case) would be:
1while True:
2 enemy1 = hero.findNearestEnemy()
3 if enemy1:
4 hero.attack(enemy1)
5 break # this would take the code to line 6.
6hero.moveUp()
7hero.moveLeft()
#this code might cause problems if the enemy had more health than one attack could do, which might lead to this solution:
1while True:
2 enemy1 = hero.findNearestEnemy()
3 if enemy1:
4 if enemy1.health > 0:
5 hero.attack(enemy1)
6 else:
7 break # this would also take the code to line 6.
8hero.moveUp()
9hero.moveLeft()
Yeah as @Deadpool198 said, you have not earned that capability yet. I do not know if it can only be earned in the glacier map, but you will not be able to end a loop in Kithgard Dungeons though
That’s not really what I said. And that’s not true exactly true either.
What I said was that I’m pretty much 100% that you can use break with promatticon 1, and I have actually just tried it with promatticon 1 in the dungeon with all the equipment I had at that point and I could use it.
It’s one of the abilities which you can use at any point, unlike a for-loop for example, which you need to have the promatticon to use.
So CodeCombat introduces break in later levels, but you can use it in the Kithgard dungeons.