You could make it return immediately if successful, and raise the error if it completes the loop without returning. (Not that I think your code is stupid as is.)