Skip to content
← Back to Community
breaking while loops
Profile icon
KianAlford

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

Voters
Profile icon
KianAlford
Profile icon
AllAwesome497
Comments
hotnewtop
Profile icon
PYer

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!

Profile icon
AllAwesome497

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

Profile icon
KianAlford

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

Profile icon
AllAwesome497

@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.

Profile icon
KianAlford

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

Profile icon
AllAwesome497

@KianAlford add dead=False at the beginning of it

Profile icon
PYer

@AllAwesome497 it is though

Profile icon
AllAwesome497

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

Profile icon
PYer

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

Profile icon
AllAwesome497

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

Profile icon
PYer

@AllAwesome497 ah. do sys.exit()

Profile icon
AllAwesome497

@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

Profile icon
AllAwesome497

you could also make it so x!=1

Profile icon
KianAlford

@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

Profile icon
VuDo

@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
Profile icon
KianAlford

@VuDo i will try this thx

Profile icon
KianAlford

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

Profile icon
Mavrick

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
Profile icon
KianAlford

@Mavrick i just explained how that will not work

Profile icon
PriyanshiBhardw

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.

Profile icon
KianAlford

@PriyanshiBhardw i will try. ill try anything anymore XD

Profile icon
PriyanshiBhardw

@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.

Profile icon
AllAwesome497

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

Profile icon
KianAlford

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

Profile icon
MATTHEWBECHTEL
Profile icon
KianAlford

@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

Profile icon
MATTHEWBECHTEL

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

Profile icon
ash15khng

Just put break multiple times with different indentation?

Profile icon
KianAlford

@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.