简体   繁体   中英

How to declare a variable outside an if statement

Here is my code:

last_state={}
last_device={}
last_substate={}
with open("TESTFILE.csv") as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        device = row[0]
        state = row[1]
        model = row[2]
        substate=row[3]
        #time = row[4]
        if device in row:

            if substate == 'Not joined' and model !='electric_meter':
                if state == "Offline":
                    N_off = [device]
                   # print N_off, reader.line_num

                if state == 'Online':
                    N_on = [device]

                if N_off == N_on:
                    print device, reader.line_num

I am trying to compare the device ID of these 2 loops so that I only use the the ones that meet all criteria. Im not sure why but I get this error:

      `if N_off == N_on:
NameError: name 'N_off' is not defined`

I am not very familiar with Python and how declaring variables works, but I tried to declare them globally and that gave me the same error. I understand it is because I am using N_off outside its scope, but I am not sure how to rectify that.

Assuming you want or need N_off to be global, you need to import the file that contains the global declaration of N_off and use it in this file as filename.N_off . In the file containing the declaration, you do not need to prefix it with the file name. Note that you do not (normally) need to declare variables in python. Global variables are an exception.

Example:

OtherFile

global N_off
N_off = value

CurrentFile

import OtherFile
if OtherFile.N_off == N_on:

Take a look at this portion of your code:

if state == "Offline":
    N_off = [device]

if state == 'Online':
    N_on = [device]

if N_off == N_on:
    print device, reader.line_num

As you can see, N_off is only defined if state == "Offline" , and N_on is only defined if state == 'Online' .

So when you go to compare them in the last if statement, Python has only defined one of them, and therefore only knows one of them.

You simply have to initialize somewhere in your program in a way that assures that they are both defined.

You have defined N_off and N_on inside if statements and only one or the other can be true and execute, Try this:

last_state={}
last_device={}
last_substate={}
with open("TESTFILE.csv") as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        device = row[0]
        state = row[1]
        model = row[2]
        substate=row[3]
        #time = row[4]
        if device in row:

            if substate == 'Not joined' and model !='electric_meter':

                N_off = None
                N_on = None

                if state == "Offline":
                    N_off = [device]
                   # print N_off, reader.line_num

                if state == 'Online':
                    N_on = [device]

                if N_off == N_on:
                    print device, reader.line_num

However I don't see the logic in this:

if state == "Offline":
    N_off = [device]
    # print N_off, reader.line_num

if state == 'Online':
    N_on = [device]

if N_off == N_on:
    print device, reader.line_num

because this:

if N_off == N_on:
    print device, reader.line_num

Will never be executed since state cannot be both on and off at the same time.

Assuming you want to loop over all the devices and want to check if consecutive rows has value of same device as "Offline" and "Online".

you can use below code, basically while checking if variables are equal, I have added additional condition to check if variables are initialized. if they are not initialized, I assumed that your condition will not be satisfied.

last_state={}
last_device={}
last_substate={}
#reader = [('device','Offline','model','Not joined')]
# Instead of file reading from list
reader = [('device','Offline','model','Not joined'),('device','Online','model','Not joined')]

for row in reader:
    device = row[0]
    state = row[1]
    model = row[2]
    substate=row[3]
    #time = row[4]

    if device in row:

        if substate == 'Not joined' and model !='electric_meter':
            print(state)
            if state == "Offline":
                N_off = [device]


            if state == 'Online':
                N_on = [device]

   # added condition to check if this variables are initialized or not.
            if 'N_on' in locals() and 'N_off' in locals() and N_off == N_on:
                print "Check passed"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM