MarcinKonowalczyk
MarcinKonowalczyk

Reputation: 2788

Python: Conditional OR

I have the following while loop:

accuracy = 0.0001    #  Or whatever
loop_limit = True
limit = 100
#  Main loop:
while abs(P - P_old).max() > accuracy or (if loop_limit: count > limit):  

Needless to say it doesn't work. What I want, is for the or statement to be checked only when loop_limit = True.

I could put an if statement into the while loop which resets the count variable on every pass of the loop only if loop_limit = True, but that would have to check loop_limit on every pass of the while loop and i want to avoid that. (The loop is going to be running millions of times over and every time saver helps). I have a feeling there is a neat way of doing this.

Edit:

Note that (P - P_old) is a numpy array, hence the .max() I forgot to mention that and i don't want to remove it now since it has been referenced in an answer already.

Upvotes: 2

Views: 1140

Answers (1)

Óscar López
Óscar López

Reputation: 235994

Try this:

while abs(P - P_old) > accuracy or (loop_limit and count > limit):

By the way, what's that max() supposed to do there? it doesn't even compile...

Upvotes: 4

Related Questions