jeudi 4 mai 2017

Cannot change variable state from within if statement

I can't change the value of a variable outside an if statement with code within that if statement.

#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)

The two variables alarm_active and alarm_on are declared global within the handle(msg) method. When foo == 'chiuso', the code within that if is not executed. Is an issue of using global variables?

Aucun commentaire:

Enregistrer un commentaire