【发布时间】:2022-01-25 17:03:52
【问题描述】:
我目前正在尝试设置此代码,以便当在这种情况下连接到 RPi GPIO 的按钮 button1 运行函数 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的麻烦,我尝试在while中添加一个break,当按下另一个按钮时没有任何运气,因为代码不会停止。
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 以打破循环。我希望通过在按下新按钮后告诉代码停止循环可以工作,但没有成功。
我在这里错过了什么?
【问题讨论】:
标签: python python-3.x raspberry-pi gpiozero