Ask coding questions

← Back to all posts
breaking while loops
KianAlford (6)

I'm currently trying to make an RP game. all I need to do is break a few while loops. but a certain amount of times every time. I can't figure out how to do it.

https://repl.it/@KianAlford/RPG-adventure-1

Commentshotnewtop
PYer (2728)

In python, you can use the break statement:
for example:

while True:
   if 1 == 2: break
   print('I does not equal 2')

Please mark this as the correct answer thanks!

AllAwesome497 (328)

@PYer wait what if it isnt the right answer :thonk:

KianAlford (6)

@AllAwesome497 I need to break multiple while loops inside one if statement

AllAwesome497 (328)

@KianAlford maybe add a if statement to all of them that breaks it if a certain codition is met?:
Ex:

if dead:
    break

that may work.

KianAlford (6)

@AllAwesome497 no because of those variables before they are not defined but only once then

AllAwesome497 (328)

@KianAlford add dead=False at the beginning of it

AllAwesome497 (328)

@PYer, first of all, it wasn't, second of all, it was a joke.

PYer (2728)

@AllAwesome497 what is the answer? he/she asked on how to break out of a while loop. i showed how to

AllAwesome497 (328)

@PYer He wants to break multiple loops within 1 if statement

AllAwesome497 (328)

@PYer I already said that. Although i just did a few minutes ago so u may not have seen it. was not on the reply thread

AllAwesome497 (328)

you could also make it so x!=1

KianAlford (6)

@AllAwesome497 no. I'm wondering how to break 2 while loops. like at the end. I need to break 3 while loops on that if statement. only if that happens

VuDo (3)

@KianAlford like others had suggested, break at different loops. You could use controlled variables. e.g. isAlive = true

while isAlive

  • while isAlive and doSomething
    #to break, set isAlive = false then use break statement
    #if necessary, use if !isAlive: break immediately after the while loop if you
    #to prevent codes not inside the loop from running
KianAlford (6)

@VuDo that would logically make no sense tho. because it only needs to tell if true or false inside that if statement. nowhere else.

Mavrick (78)

Add multiple break statements is a solution to go down a controlled number of steps:

x = True
while True:
     # stuff
     while True:
          # stuff
          if x == True:
               break
     if x == True:
          break
#more stuff
KianAlford (6)

@Mavrick i just explained how that will not work

PriyanshiBhardw (0)

You try using a flag variable(like concept ) like just make it true whenever you want to break a loop and that flag variable gets checked for each iteration. I think it might help.

KianAlford (6)

@PriyanshiBhardw i will try. ill try anything anymore XD

PriyanshiBhardw (0)

@KianAlford look I am not familiar with python.can you just brief me a little more about your problem in general way.I might give you another suggestion also.

AllAwesome497 (328)

Alternatively what you could do if u are not doing anything after the loop, you could add import sys to the top of the file and use sys.exit() which ends the program

KianAlford (6)

@AllAwesome497 i also need it to break only that set of while statements. not the entire code.

KianAlford (6)

@MATTHEWBECHTEL i dont think you know that i know how to break while statements. i know how to do that. im just stuck on a double break

MATTHEWBECHTEL (123)

Oh ok sorry I can’t help you with that. @KianAlford

ash15khng (561)

Just put break multiple times with different indentation?

KianAlford (6)

@ash15khng tried that. does not work sadly.
mainly cause its not in the same if statement.
it acts as if it were in the other while statements still even tho its farther down the code.