0

I currently have two if statements that look for text in a string

if "element1" in htmlText:
    print("Element 1 Is Present")
    return

if "element2" in htmlText:
    print("Element 2 Is Present")
    return

These both work great, what I would now like to do is add an if statement that checks if element3 is present, but neither element1 or element2 are present

How do I chain these 3 checks together, is there an AND operator like in PHP?

2
  • Something like this? How do I test one variable against multiple values? Commented Mar 1, 2016 at 14:28
  • There's and, of course, (which is literally and). But if I get you right, you want "this condition and not the other conditions"? Commented Mar 1, 2016 at 14:31

6 Answers 6

4

Since return will return when a match was previously found, it's enough to append this code:

if "element3" in htmlText:
    print("Element 3 Is Present")
    return
Sign up to request clarification or add additional context in comments.

Comments

1

Try:

if "element1" in htmlText:
    print("Element 1 Is Present")
    return

elif "element2" in htmlText:
    print("Element 2 Is Present")
    return

elif "element3" in htmlText:
    print("Element 3 Is Present")
    return

1 Comment

The return statements become unnecessary with the elif
1

Ofcourse in python there is and operator.

if "element1" in htmlText and "element2" in htmlText:
  do something 

OR you can still stick with your previous logic

if "element1" in htmlText :
    do...something
elif "element2" in htmlText :
    do something

elif "element3" in htmlText :
    do something 

else: 
   do other things

Comments

0

None of the other answers directly address this statement...

I would now like to do is add an if statement that checks if element3 is present, but neither element1 or element2 are present

Which can be written as

if "element3" in htmlText and not ("element2" in htmlText or "element1" in htmlText):

Comments

0

Early return (checking conditions in the right order, see given answers) is usually to be preferred performance wise.

If you cannot make use of early return, but instead need arbitrary conditions on the elements, remember that you have (list/dict) comprehensions.

For example

contains_matrix = [
   (element in htmlText)
   for element in ("element1", "element2", "element3")
]

will yield a list with True and False for each of the elements. The condition you mention in the question can then be formulated as

not contains_matrix[0] and not contains_matrix[1] and contains_matrix[2]

Let me repeat: the same result can be achieved by checking for "element3" last and returning early.

Dictionaries are even nicer (and more pythonic):

contains_dict = {
    element: (element in htmlText)
    for element in ("element1", "element2", "element3")
}

Evaluate with:

(
    not contains_dict['element1'] 
    and not contains_dict['element2'] 
    and contains_dict['element3']
)

or even

[element for element, contained in contains_dict.items() if contained]

which gives you all elements that are contained in the HTML.

Comments

0

I think this would be the most scalable solution:

elementsToCheck = ['element1','element2','element3']
for eIdx, eChk in enumerate(htmlText):
    if eChk in htmlText:
        print "Element {0} Is Present".format(eIdx)
        return

to answer the original question (though as has been pointed out before it is not needed to check against the other 2 elements):

if 'element3' in htmlText and not ('element1' in htmlText or 'element2' in htmlText):
   print "Element 3 Is Present"
   return

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.