我尝试在内部循环中循环,当开始第二个循环时,第一个立即停止。我需要无休止地检查RELE1是否为True,并且只需要打印“ alarm”(警报)一次,因为我第二次循环检查警报0或1。这是我的代码:
while True:
if (GPIO.input(RELE1) == True):
print('3.3')
GPIO.setup(RELE360, GPIO.OUT)
alarm = 0
while 1:
if (GPIO.input(RELE0) == True):
alarm += 1
if(alarm == 1 ):
print('alarm')
else:
alarm = 0
else:
print('0')
GPIO.setup(RELE360, GPIO.IN)
sleep(1);
答案 0 :(得分:1)
您有一个无限循环。这将永远不会终止:
while True:
if (GPIO.input(RELE1) == True):
print('3.3')
GPIO.setup(RELE360, GPIO.OUT)
alarm = 0
while 1:
if (GPIO.input(RELE0) == True):
alarm += 1
if(alarm == 1 ):
print('alarm')
else:
alarm = 0
# -- you will never escape this loop! --
else:
print('0')
GPIO.setup(RELE360, GPIO.IN)
sleep(1);
您可以删除循环,也可以break
对其进行转义。
while True:
if some_condition:
break # this will exit the loop
或者,您可以将第二个循环滚动到第一个循环中;通过查看您的代码,看来您可以这样做:
alarm = 0
while True:
if (GPIO.input(RELE1) == True):
print('3.3')
GPIO.setup(RELE360, GPIO.OUT)
if (GPIO.input(RELE0) == True):
alarm += 1
if(alarm == 1 ):
print('alarm')
else:
print('0')
GPIO.setup(RELE360, GPIO.IN)
sleep(1);