Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

How to Stop the while True loop?

#1

Hello everyone!

I’m at the end of the first level (Kithgard’s Dungeon) but I’m still having trouble with the While True Loop. :frowning: 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!

Thank you very much.

Catopatra

1 Like
#2

Do:

while True:
    enemy1 = hero.findNearestEnemy()
        if enemy1:
            hero.attack(enemy1)
        else:
            hero.moveRight()
1 Like
#3

Thank you very much! But I have a question with your answer.

If I write a lot of code after the “else” part, should it still have the 4 spaces before each code? What happens then?

Like this:

while True:
    enemy1 = hero.findNearestEnemy()
        if enemy1:
            hero.attack(enemy1)
        else:
            hero.moveRight()
            hero.moveLeft()
            hero.moveUp()

OR

like this:

while True:
    enemy1 = hero.findNearestEnemy()
        if enemy1:
            hero.attack(enemy1)
        else:
            hero.moveRight()
hero.moveUp()
hero.moveLeft()

Thank you SO much for your help!

  • Catopatra
#4

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

I hope this helps,
:lion: :lion: :lion:

1 Like
#5

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

#6

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.
:lion: :lion: :lion:

1 Like
#7

Sorry bro. I misunderstood you. My bad

#8

It’s fine, don’t worry. :grin:
:lion:

1 Like
#9

Is there a way to use the while true loop and then continue to another line of code, not in a while true loop? This is one of two questions. :thinking:

#10

Is this something like what you mean?

while True:
    if something:
        do this
    else:
        break