I'm working with code that looks something like this:
from time import sleep
from gpipozero import Button
BUTTONS = {
'start': Button(21),
'choice_1': Button(1),
'choice_2': Button(7),
}
CHOICE = 0
def do_stuff():
global CHOICE
# wait for user to make a choice
while CHOICE == 0:
sleep(0.2)
if CHOICE == 1:
# do variant a
elif CHOICE == 2:
# do variant b
CHOICE = 0
def assign_choice_1():
global CHOICE
print("chose 1")
CHOICE = 1
def assign_choice_2():
global CHOICE
print("chose 2")
CHOICE = 2
BUTTONS['start'].when_pressed = do_stuff
BUTTONS['choice_1'].when_pressed = assign_choice_1
BUTTONS['choice_2'].when_pressed = assign_choice_2
What I observe is that the assign_choice_x
functions don't seem to be working. There is no printout, and the script does not continue.
My questions:
What's the reason this doesn't work.
Regardless of 1, I feel like this approach is an ugly hack. What's a better way to achieve what I want. That is, user initiates routine with a button press, then proceeds to make a choice with another button press. That choice is stored and used in the rest of the routine.