I am currently trying to setup this code so that when a button in this case button1
connected to a RPi GPIO runs the function c1
and keeps looping that function until another button button2
is pressed and then it runs the function c2
keep looping in that function.
#Import RPi GPIO module
from gpiozero import Button
#Assign name to buttons GPIO pins
button1 = Button(2)
button2 = Button(4)
def c1():
while True:
print('c1')
grade = float(input('Select grade: '))
if grade < 10:
print('Less than 10')
else:
print ('invalid input')
def c2():
while True:
print('c2')
grade = float(input('Select grade: '))
if grade < 10:
print('Less than 10')
else:
print ('invalid input')
I encountered troubles breaking the function c1
, I have tried to add a break
in the while
when another button is pressed as follows without any luck as the code doesn't stop.
def c1():
while True:
print('c1')
grade = float(input('Select grade: '))
if grade < 10:
print('Less than 10')
elif button2.is_pressed:
break
I have also tried this to break the loop but I'm not even sure it is a correct way to do it, anyhow it did not work.
def c1():
while True:
print('c1')
grade = float(input('Select grade: '))
if grade < 10:
print('Less than 10')
elif button2.is_pressed:
c1() == False
break
I am not sure to be correct but I have a feeling that something has to change the function let's assume c1
to False
to break the loop. I was hoping that by telling the code once a new button is pressed stop the loop would work but it didn't work out.
What am I missing here?