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.
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.
@PriyanshiBhardw i will try. ill try anything anymore XD
@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.
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
@AllAwesome497 i also need it to break only that set of while statements. not the entire code.
@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
Oh ok sorry I can’t help you with that. @KianAlford
Just put break multiple times with different indentation?
@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.
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!
@PYer wait what if it isnt the right answer :thonk:
@AllAwesome497 I need to break multiple while loops inside one if statement
@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.
@AllAwesome497 no because of those variables before they are not defined but only once then
@KianAlford add dead=False
at the beginning of it
@AllAwesome497 it is though
@PYer, first of all, it wasn't, second of all, it was a joke.
@AllAwesome497 what is the answer? he/she asked on how to break out of a while loop. i showed how to
@PYer He wants to break multiple loops within 1 if statement
@AllAwesome497 ah. do sys.exit()
@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
you could also make it so x!=1
@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
@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
@VuDo i will try this thx
@VuDo that would logically make no sense tho. because it only needs to tell if true or false inside that if statement. nowhere else.
Add multiple
break
statements is a solution to go down a controlled number of steps:@Mavrick i just explained how that will not work