Dec-17-2020, 05:38 PM
(Dec-17-2020, 05:31 PM)buran Wrote: you need to declare it global.
Flag = 0 nuserySWdatabase = 0 # this would be from database but is set to 0 for testing def nuseryControlFunction(nuserySWdatabase): global Flag if ( nuserySWdatabase == 0 and Flag == 0): print ("pump on") Flag = 1 print Flag else: print ("pump off") Flag = 0 while 1: nuseryControlFunction(nuserySWdatabase) print FlagYet way way better would for your function to return theflag
. If you don't change it inside the function, you can accessflag
from global scope.
nuserySWdatabase = 0 # this would be from database but is set to 0 for testing flag = False def nuseryControlFunction(nuserySWdatabase): if nuserySWdatabase == 0 and not flag: print("pump on") return True else: print("pump off") return False while True: flag = nuseryControlFunction(nuserySWdatabase) print(flag)But still better would be to if your function hasflag
as second parameter (if you need it at all - from your code I cannot decide if it really needed).
nuserySWdatabase = 0 # this would be from database but is set to 0 for testing flag = False def nuseryControlFunction(nuserySWdatabase, flag): if nuserySWdatabase == 0 and not flag: print("pump on") return True else: print("pump off") return False while True: flag = nuseryControlFunction(nuserySWdatabase, flag) print(flag)Also, note your names do not comply with PEP8 recommendations but I kept them
Thanks for the quick reply, I ended up going with the return script as this looks better and makes more sense.
Not sure what the PEP8 naming is, I will have to look this up. Most of my variable naming comes from where I do jQuery and php.
As for not having the Flag in the end I will be adding in time to the ON IF statement so if the pump is on for too long then the program will go into a shutdown status. So within the pump on statement a time of when it comes on will be stored, so this needs to only happen once