Use of "or"

I don’t know why but I have never used or in any of my if statements, but I decided recently that I would try it to save some lines and have run into making it function.


Var = ("First", "Second", "Third")

for objs in Var:
    if objs == "Second" | "Third":
        print "Success"

here is the error that is thrown.


TypeError: unsupported operand type(s) for |: 'str' and 'str' # 

I know someone here has to have an idea of how that works. thanks!

[QUOTE=Bharris;3305]I don’t know why but I have never used or in any of my if statements, but I decided recently that I would try it to save some lines and have run into making it function.


Var = ("First", "Second", "Third")

for objs in Var:
    if objs == "Second" | "Third":
        print "Success"

here is the error that is thrown.


TypeError: unsupported operand type(s) for |: 'str' and 'str' # 

I know someone here has to have an idea of how that works. thanks![/QUOTE]

are you sure you’re not meaning to use “||”?

I think this is what you want:


for objs in Var:
    if objs == "Second" or objs == "Third":
        print "Success"

The | operator does exist, but it’s a “bitwise or” for dealing with sequences of bits. “or” is the more normal “logical or”.

The other issue is a bit subtler. If you write [pre]objs == “Second” or “Third”[/pre] then Python interprets it as [pre](objs == “Second”) or (“Third”)[/pre]Since “Third” is a non-empty string it’s considered to be always true, so your if statement would always execute! Writing it as [pre](objs == “Second”) or (objs == “Third”)[/pre] does the right thing, and the brackets can be dropped if you like.

[QUOTE=Tim Rennie;3309]I think this is what you want:


for objs in Var:
    if objs == "Second" or objs == "Third":
        print "Success"

The | operator does exist, but it’s a “bitwise or” for dealing with sequences of bits. “or” is the more normal “logical or”.

The other issue is a bit subtler. If you write [pre]objs == “Second” or “Third”[/pre] then Python interprets it as [pre](objs == “Second”) or (“Third”)[/pre]Since “Third” is a non-empty string it’s considered to be always true, so your if statement would always execute! Writing it as [pre](objs == “Second”) or (objs == “Third”)[/pre] does the right thing, and the brackets can be dropped if you like.[/QUOTE]

Ok. This is what I’m looking for. Thanks. This should be helpful.

Seth, you may be correct about what I was thinking. As I said I had never used it.

knobjackasshavenhasslegherkin