我目前正在尝试设置此代码,以便在这种情况下button1
连接到 RPi GPIO 的按钮运行该功能c1
并继续循环该功能,直到按下另一个按钮button2
,然后它运行该功能c2
继续在该功能中循环。
#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')
我遇到了破坏功能的麻烦c1
,我试图break
在while
按下另一个按钮时添加一个,如下所示,因为代码没有停止,所以没有任何运气。
def c1():
while True:
print('c1')
grade = float(input('Select grade: '))
if grade < 10:
print('Less than 10')
elif button2.is_pressed:
break
我也试过这个来打破循环,但我什至不确定这是一个正确的方法,无论如何它没有工作。
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
我不确定是否正确,但我有一种感觉必须改变函数让我们假设c1
打破False
循环。我希望通过在按下新按钮后告诉代码停止循环将起作用,但它没有成功。
我在这里想念什么?