Python 'if' condition doesn't work with shorthand 'or' [duplicate]


Python 'if' condition doesn't work with shorthand 'or' [duplicate]



This question already has an answer here:



What is wrong here on the method a|b==0 vs a==0 or b==0 ?


def validate(a,b,c):
print(a,b,c)
if (a|b == 0) or c == 0:
return "Invalid"
else:
return "Valid"
print("Test 1: ", validate(0,1,2))
print("Test 2: ", validate(0,1,0))


>0 1 2
Test 1: Valid


>0 1 0
Test 2: Invalid



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





There's no such "shorthand or" syntax in Python. | is a bitwise OR operator.
– user2357112
Jul 1 at 23:44


|





Where did you even get that idea? Perl 6?
– user2357112
Jul 1 at 23:45





The pipe symbol | represents bitwise or, are you looking for logical or? (literal or).
– jedwards
Jul 1 at 23:45


|


or





@eyllanesc although this question is framed as a duplicate of this question - it seems to be an misunderstanding about how chained logic operators work in python. I suspect it's still a duplicate - but I don't think the target you've given is the best possible one.
– Shadow
Jul 1 at 23:51






@Shadow Why is my decision wrong? I see that many of the answers answer the difference between the two, I think that you are only seeing the question marked as correct and not the others
– eyllanesc
Jul 1 at 23:56




1 Answer
1



You seem to have confused python's or and |.


or


|



Understandable, given that the or operator in many langages is | (or more commonly ||).


|


||



This page lists python's operators



Try your example again, but using or instead of |


or


|


if (a or b == 0) or c == 0:



If I'm reading your question correctly however, I think you're trying to do this;


if (a == b == 0) or c == 0:



This is the shortcut-syntax in python for checking if both a and b is equal to 0.


a


b



EDIT: As per clarification;



Requirement is to confirm 0 is not passed as an argument to the function



If you can safely assume that your arguments are going to be ints, then I would take the following approach:


if a and b and c:
pass # valid
else:
pass # invalid



0 is falsey - so lets use the fact. Otherwise, there isn't really a 'shortcut' way to do what you're doing.


0





thanks, I understand the mistake here. But trying with your first example, it still shows the same error def validate(a,b,c): print(a,b,c) if (a or b == 0) or c == 0: return "Invalid" else: return "Valid" print("Test 1: ", validate(0,1,2)) print("Test 2: ", validate(0,1,0)) >>0 1 2 Test 1: Valid 0 1 0 Test 2: Invalid
– ɐ ɯıɥɐɹ
Jul 2 at 0:02



def validate(a,b,c): print(a,b,c) if (a or b == 0) or c == 0: return "Invalid" else: return "Valid" print("Test 1: ", validate(0,1,2)) print("Test 2: ", validate(0,1,0))





Just switching or for | doesn't fix this code, since or doesn't work like that either.
– user2357112
Jul 2 at 0:15


or


|


or





@ɐɯıɥɐɹ What about the second part of my answer? If that isn't doing what you think it should, then I think it might be a good idea to edit your question and state exactly when this should be true and false with examples of both.
– Shadow
Jul 2 at 0:24





Second suggestion works like and and only if both a and b are 0. Here is what works for me if a ==0 or b == 0 or c == 0 or if 0 in (a,b,c) . Requirement is to confirm 0 is not passed as an argument to the function
– ɐ ɯıɥɐɹ
Jul 2 at 1:59



and


if a ==0 or b == 0 or c == 0


if 0 in (a,b,c)

Popular posts from this blog

JMeter fails on beanshell imports

Why in node-red my HTTP POST no receive payload from inject?

PHP contact form sending but not receiving emails