Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global - local variables
#3
(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 Flag
Yet way way better would for your function to return the flag. If you don't change it inside the function, you can access flag 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 has flag 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
Reply


Messages In This Thread
Global - local variables - by Motorhomer14 - Dec-17-2020, 04:45 PM
RE: Global - local variables - by buran - Dec-17-2020, 05:31 PM
RE: Global - local variables - by Motorhomer14 - Dec-17-2020, 05:38 PM
RE: Global - local variables - by buran - Dec-17-2020, 05:39 PM
RE: Global - local variables - by Motorhomer14 - Dec-17-2020, 05:50 PM
RE: Global - local variables - by buran - Dec-17-2020, 06:04 PM
RE: Global - local variables - by Motorhomer14 - Dec-17-2020, 06:11 PM
RE: Global - local variables - by Motorhomer14 - Dec-17-2020, 06:24 PM
RE: Global - local variables - by buran - Dec-17-2020, 06:27 PM
RE: Global - local variables - by buran - Dec-17-2020, 06:32 PM
RE: Global - local variables - by deanhystad - Dec-17-2020, 06:35 PM
RE: Global - local variables - by Motorhomer14 - Dec-17-2020, 06:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,186 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Trying to understand global variables 357mag 5 1,143 May-12-2023, 04:16 PM
Last Post: deanhystad
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,805 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Global variables or local accessible caslor 4 1,040 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,162 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  How to use global value or local value sabuzaki 4 1,167 Jan-11-2023, 11:59 AM
Last Post: Gribouillis
  Clarity on global variables JonWayn 2 960 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  Global variables not working hobbyist 9 4,754 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 2,673 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  from global space to local space Skaperen 4 2,335 Sep-08-2020, 04:59 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020