Looking at the code, it sounds like the intent is to leave as soon, as the condition is met. If so, I would recommend the sleep to be way smaller - e.g. 50ms, so that it doesn't visibly slow down the program. 0.5s as is now is still visible by the user. On the other side, if you want the user to see what is happening, you may keep it to 0.5 seconds, but then you will need to put the sleep before checking for exit condition to ensure that the code always pauses first.