Duohead Duohead - 3 months ago 9
Python Question

While loop check one condition before the other

I'm new to programming, and I want to write some code like

while(condition_A and condition_B):
#Do something


But each time I run the while loop, I want to check condition A first, and if condition A works then check condition B. For example, condition A checks if condition B will get an array out of bounds error or something. And finally if both conditions are true stay in the while loop. How should I do this? I was thinking of something like

def some_While_Loop:
if condition_A == False:
return
while (condition_B):
#Do something
if condition_A == False:
return


But then the while loop has to be the last thing a function does. Is there a nicer/better way?

Answer

It looks like you're using python. You actually had the answer yourself:

while(condition_A and condition_B):  
    #Do something