For loop execution issue - bug?

I have a for loop that has an if statement nested in it inside of a script. The for loop does not appear to be executing correctly, as the for loop iterates before trying to enter the if loop. This is in a script in a binding in Perspective.

I have attached my code, and the console output.

I am currently using Ignition Platform 8.0.12 and Perspective Module 1.0.12.

Your if statements are not going to work as I’m guessing you intended.

If you want to test a value against 2 or more conditions you would have to check them all explicitly like this.

if x == 2 or x == 3 or x == 4:
    # Do something

Your 1st if statement evaluates if dataData[rows][1] is equal to ‘10015531’ and then it evaluates your next item (‘10023468’) as a statement to see if it can be evaluated as True or not and does that for all of the remaining items.

In your case you may be able to do something like this to make it a little cleaner. I left out some of the conditions.

if dateData[rows][1] in ['10015531', '10023468', '10036077', '10026408', '10010805', '10033990', '10010888']:
    # Do Something

What @JGJohnson said.

'x' == 'y' or 'z'

is a conditional evaluation which will result in True in Python because ‘z’ is not False or None. The Pythonic way to determine if a given value is within a set of values is to use

x in someList
1 Like

In addition to what @JGJohnson and @cmallonee said, you’re not taking full advantage of the pyDataSet model (assuming dateData is a pyDataSet

Your loop should look something like this

build = (‘10015531’,’10023468’)
warehouse = (‘10015539’,’10036724’)

for row in dateData:
    if row[1] in build:
        scheduledBuildDate = row[2]
        actualBuildDate = row[3]
    elif row[1] in warehouse:
        scheduledWarehouseDate = row[2]
        actualWarehouseDate = row[3]

To make it even more readable you can use the column name

So say that the header for column1 is Code, column 2 is Scheduled, and column 3 is Actual. Then your code would read.

build = (‘10015531’,’10023468’)
warehouse = (‘10015539’,’10036724’)

for row in dateData:
    if row[‘Code’] in build:
        scheduledBuildDate = row[‘Scheduled’]
        actualBuildDate = row[‘Actual’]
    elif row[‘Code’] in warehouse:
        scheduledWarehouseDate = row[‘Scheduled’]
        actualWarehouseDate = row[‘Actual’]