Scripting - IF ELSE

Hi all,

I am learning Ignition, scripting, and Databases all at the same time and I am stuck.

I am trying to add an if else statement to a script I have so I set up a window just to play with. I have 2 text fields and a button with this actionPerformed script.

Line 6-7 never works. if name1 is not empty, the Text Field gets the name1 value.

Something is wrong with line 5 or 6 but I have tried all I can guess to try and can’t make it work.

What am I missing?

Thanks for the help!!

Steven

if name1 == "":

In Python, is tests if two variables refer to the same object

Also you can drop the semicolons, although they won’t hurt anything

For bonus points use a ternary operator. Delete from row 5 on and do

name = name1 if name1 else username

@chasondeshotel Thank you !! That was my original syntax, but chasing other issues made me change it and I guess I never went back to it when I corrected the other issue.

Thanks for the help!!!

Steven

1 Like

@chasondeshotel,

following up here…how do I continue to run the code after the if else statement? Seems like a LONG time ago it used to be END IF:. Seems things have changed a lot since then.

so…

if this is true
do this
else
do that
then keep doing
the
consecutive
lines to the end.

Thanks, Steven

Unless you specifically call a return or something of that nature the code will continue past each if.
For multiple you should use the elif as well.

name1 = event.source.parent.getComponent('Text Field').text
if name1:
    if name1=='Bob':
        event.source.parent.getComponent('Text Field 1').text = 'Bob'
    elif name1=='Joe':
        event.source.parent.getComponent('Text Field 1').text = 'Joe'
    else:
        event.source.parent.getComponent('Text Field 1').text = name1
else:
    event.source.parent.getComponent('Text Field 1').text = 'Nothing Entered'
1 Like

In Python, whitespace is used to denote scope… so levels of indenting matter. In this example, when your indention level recedes one, the interpreter knows you’ve exited the if block. This is also the reason you don’t normally use semicolons in Python

And for more bonus points, Python’s for loop also has an else. It’s executed only if there was not a break condition

for thing in iterable:
    if find_thing(thing):
        # found it!
        do_stuff(thing)
        break
else:
    # he ain't here ._.
    thing_not_found()

Which can be handy and almost makes up for their terrible ternary syntax

@chasondeshotel so I don’t even need the else. That works just like I wanted.

Thanks for the examples. I am sure I will put all this to good use in the near future.

Thanks, Steven

1 Like