While loop help


I just purchased a beaglebone black and have just began tinkering around with it. I have gotten a button to turn on and off a led light but wanted to alter this a bit. I have written some code to turn the led on after the first push, blink after the second, and turn off after the third. The code has some issues because depending on the length of the time.sleeps and the time the third button push occurs the led may not turn off. I have attached a screenshot of the code I have. What can I do to better pick up the third button push?