Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If and/or statement
#1
Hello,

I want to construct an if statement using or/and.  If either string value exists in the first element in a list and then look to see if has_signoff == True?

I've tried what I have below and it seems to be right, but I want to make sure (like using some sort of grouping)?

if warning_message in build_status_warning_lst[0] or succeeded_message in build_status_warning_lst[0] and has_signoff == True
Reply
#2
Insert test and print statement:

warning_message = 'Warning! Warning!'
succeeded_message = 'Life is good'
has_signoff = True

build_status_warning = ['Warning! Warning!', 'Arms waving up and down']

if warning_message in build_status_warning or succeeded_message in build_status_warning and has_signoff:
    print('Good to go!')
else:
    print('Warning Warning Warning')
Reply
#3
Thanks.  I did that and it looks like what I have works.  I was just worried about some odd outside case and if I needed to group the or statement in parenthesis.
Reply
#4
Although i would write it like this:

build_status_warning = ['Warning! Warning!', 'Arms waving up and down']

def check_errors(*msgs):
    retval = False
    for msg in msgs:
        if msg in build_status_warning:
            retval = True
    return retval

def main():
    warning_message = 'Warning! Warning!'
    succeeded_message = 'Life is good'
    has_signoff = True

    if check_errors(warning_message, succeeded_message) and has_signoff:
        print('Good to go!')
    else:
        print('Warning Warning Warning')

if __name__ == '__main__':
    main()
Reply
#5
(Nov-04-2016, 07:08 PM)DBS Wrote: Hello,

I want to construct an if statement using or/and.  If either string value exists in the first element in a list and then look to see if has_signoff == True?

I've tried what I have below and it seems to be right, but I want to make sure (like using some sort of grouping)?

if warning_message in build_status_warning_lst[0] or succeeded_message in build_status_warning_lst[0] and has_signoff == True

You should use parenthases then, as and has higher precidence than or, so what you wrote is the same as this:
if warning_message in build_status_warning_lst[0] or (succeeded_message in build_status_warning_lst[0] and has_signoff == True)
Another example, without variables, to demonstrate:
>>> True or True and False
True
>>> True or (True and False)
True
>>> (True or True) and False
False
Reply


Forum Jump:

User Panel Messages

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