해당 if 문 내의 코드를 사용하여 if 문 외부의 변수 값을 변경할 수 없습니다.if 문 내에서 변수 상태를 변경할 수 없습니다.
#import lines here
alarm_active=False
alarm_on=False
def handle(msg):
global alarm_active
global alarm_on
#other lines of code
while 1:
print 'while works'
if alarm_active==True:
print 'alarm works'
foo = subprocess.check_output("python one_sensor_alarm.py", shell=True)
print foo
if foo == 'chiuso':
print "intrusion"
alarm_on=True
alarm_active=False
time.sleep(1)
두 변수와 alarm_on
alarm_active
는 handle(msg)
있어서 내의 글로벌 선언된다. foo == 'chiuso'
인 경우 해당 코드는 실행되지 않습니다. 전역 변수를 사용하는 데 문제가 있습니까?
제대로 while 루프를 들여되지 않은 : 그래서이 코드를 사용해보십시오. 또한 왜 'while while : while while : 1 :'을하고 있습니까? – snb
'check_output'이 정확히 "chiuso"를 반환하는지 확인하십시오. "chiuso \ n"? 너 해봤 어? –
@snb 동일하지만, 당신 말이 맞습니다. True는 더 읽기 쉽습니다. 감사합니다 안톤, 문제는 내가 깜빡했다. \ n :) – Caosman