Dec-17-2020, 04:45 PM
Hello All
I am struggling with using local or global variables. I want to change a variable from 0 to 1 and vis versa within a function when something changes. If I set the variable as a global variable then it is always reset to 0 when the function is called. If I do not declare it globally then it will produce an error of not declared. The other option is to declare it locally before my IF statement but it would then be set to the declared state each time. Hope this makes some sense. The test code is shown below. The If statement is only to be called once, hence the Flag variable
I am struggling with using local or global variables. I want to change a variable from 0 to 1 and vis versa within a function when something changes. If I set the variable as a global variable then it is always reset to 0 when the function is called. If I do not declare it globally then it will produce an error of not declared. The other option is to declare it locally before my IF statement but it would then be set to the declared state each time. Hope this makes some sense. The test code is shown below. The If statement is only to be called once, hence the Flag variable
Flag = 0 nuserySWdatabase = 0 # this would be from database but is set to 0 for testing def nuseryControlFunction(nuserySWdatabase): if ( nuserySWdatabase == 0 and Flag == 0): print ("pump on") Flag = 1 print Flag else: print ("pump off") Flag = 0 while 1: nuseryControlFunction(nuserySWdatabase) print Flag