According to the code above, the "while True" does the following, in order. I'm ignoring the "sleep()" calls for this.
To get a better idea, you'd need to edit the question to add a circuit for your buttons and LED. The only thing this might change in my answer is if the button is high when it is pressed, or low when it is pressed.
- Turn on the LED
- Turn off the LED
- Is pin 2 true?
- If so, then turn on the LED
These same four steps keep being executed in order, until you stop the program.
You need to change step four. I think the best way to do this is to break out of the while loop (with a break statment) and create a second while loop right after the first, which just has "while True:" and doesn't do anything but that. It's just there to keep the program from ending.
What I'd do is to rewrite the end of your first while loop as:
... Turn the LED on and off ...
if GPIO.input(2) == True:
break // exit the while loop
GPIO.output(17, True) // turn on the LED
// Don't do anything
That's it. A second while loop will keep the LED on. You don't need a variable to store the button state information because you only check it once.