how to terminate loop in python