Python 没有移动过去的 while 循环
Python not moving past while loop
美好的一天,
我有一个脚本,用于监视来自 2 个运动传感器的输入,如果检测到来自这 2 个传感器中的任何一个的运动,该传感器将打开灯 (Raspberry Pi 4)。在 while 循环之后,我还有更多代码需要执行,但是,python 不会 move/continue 通过 while 循环。
代码如下:
from datetime import datetime
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
rightMotionActivity = 3
leftMotionActivity = 5
flashLightControl = 7
GPIO.setup(flashLightControl, GPIO.OUT)
GPIO.setup(leftMotionActivity, GPIO.IN)
GPIO.setup(rightMotionActivity, GPIO.IN)
while True:
leftMotionDetect = GPIO.input(5)
rightMotionDetect = GPIO.input(3)
# If both motion sensors are 0
if leftMotionDetect==0 and rightMotionDetect==0:
GPIO.output(flashLightControl, GPIO.LOW)
time.sleep(0.1)
# If either of the motion sensors are 1
elif leftMotionDetect==1 or rightMotionDetect==1:
GPIO.output(flashLightControl, GPIO.HIGH)
time.sleep(0.1)
# Rest of the code follows but python does not get here
while True
表示它会在 true 时继续,意味着永远,因此它不会执行任何其他代码。如果您希望它随时停止,请将 while 条件更改为其他条件。
通常,while
循环在循环的顶部有一个条件,并继续循环直到不再满足该条件。
对于,while True
,我们可以看出这个条件总是True
。因此,跳出循环的唯一方法是使用break
。
所以你可以设置一个选择语句,如果满足就会跳出循环......或者你可以改变顶部的条件。
由于 while True
,while 循环将永远不会停止 运行。您需要实现一种方式来摆脱 while 循环。
像这样:
loopReady = True
while loopReady
if(sensor1 and sensor2)
loopReady = false
#rest of the code
如果sensor1和sensor 2为'True',loopReady会变为false,while循环退出
美好的一天,
我有一个脚本,用于监视来自 2 个运动传感器的输入,如果检测到来自这 2 个传感器中的任何一个的运动,该传感器将打开灯 (Raspberry Pi 4)。在 while 循环之后,我还有更多代码需要执行,但是,python 不会 move/continue 通过 while 循环。
代码如下:
from datetime import datetime
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
rightMotionActivity = 3
leftMotionActivity = 5
flashLightControl = 7
GPIO.setup(flashLightControl, GPIO.OUT)
GPIO.setup(leftMotionActivity, GPIO.IN)
GPIO.setup(rightMotionActivity, GPIO.IN)
while True:
leftMotionDetect = GPIO.input(5)
rightMotionDetect = GPIO.input(3)
# If both motion sensors are 0
if leftMotionDetect==0 and rightMotionDetect==0:
GPIO.output(flashLightControl, GPIO.LOW)
time.sleep(0.1)
# If either of the motion sensors are 1
elif leftMotionDetect==1 or rightMotionDetect==1:
GPIO.output(flashLightControl, GPIO.HIGH)
time.sleep(0.1)
# Rest of the code follows but python does not get here
while True
表示它会在 true 时继续,意味着永远,因此它不会执行任何其他代码。如果您希望它随时停止,请将 while 条件更改为其他条件。
通常,while
循环在循环的顶部有一个条件,并继续循环直到不再满足该条件。
对于,while True
,我们可以看出这个条件总是True
。因此,跳出循环的唯一方法是使用break
。
所以你可以设置一个选择语句,如果满足就会跳出循环......或者你可以改变顶部的条件。
由于 while True
,while 循环将永远不会停止 运行。您需要实现一种方式来摆脱 while 循环。
像这样:
loopReady = True
while loopReady
if(sensor1 and sensor2)
loopReady = false
#rest of the code
如果sensor1和sensor 2为'True',loopReady会变为false,while循环退出