-2

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
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • 4
    There's no such "shorthand or" syntax in Python. `|` is a bitwise OR operator. – user2357112 Jul 01 '18 at 23:44
  • 1
    Where did you even get that idea? Perl 6? – user2357112 Jul 01 '18 at 23:45
  • 3
    The pipe symbol `|` represents *bitwise* or, are you looking for *logical* or? (literal `or`). – jedwards Jul 01 '18 at 23:45
  • @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 01 '18 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 01 '18 at 23:56
  • @eyllanesc that's fair, I stand corrected. Answers such as [this](https://stackoverflow.com/a/25949622/1594286) do indeed address this confusion. – Shadow Jul 01 '18 at 23:59

1 Answers1

-1

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

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 |

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.

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.

Shadow
  • 8,749
  • 4
  • 47
  • 57
  • 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 02 '18 at 00:02
  • Just switching `or` for `|` doesn't fix this code, since `or` doesn't work like that either. – user2357112 Jul 02 '18 at 00:15
  • @ɐɯıɥɐɹ 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 02 '18 at 00: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 02 '18 at 01:59