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?

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
  • Something like this? [How do I test one variable against multiple values?](http://stackoverflow.com/q/15112125) – Bhargav Rao Mar 01 '16 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"? – dhke Mar 01 '16 at 14:31

6 Answers6

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
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
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
jhoepken
  • 1,842
  • 3
  • 17
  • 24
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
pitaside
  • 650
  • 10
  • 14
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):
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
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.

dhke
  • 15,008
  • 2
  • 39
  • 56
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
dabhand
  • 517
  • 3
  • 10