maxiter = 10000
while True:
    if stopCondition():
        break
maxiter = maxiter - 1
if maxiter <= 0:
    print('Did not converge.', file=sys.stderr)
break
How to jump out of a dead loop automatically in Python?