Much to unpack here...
coin == 25 | 10 | 5
...will evaluate as True if coin is equal to the bitwise OR of 25, 10 and 5 - i.e. 31. In other word, it's equivalent to coin == 31
. That's because the bitwise OR has precedence over the == operator. See operator precedence in Python.
If I replace the β|β with βorβ the code runs just fine.
It probably doesn't. If you replace |
with or
, you have the statement coin == 25 or 10 or 5
which is always True in the if
statement because it's evaluated as (coin == 25) or (not 0) or (not 0)
in an if
statement.
coin == 25 | coin == 10 | coin == 5
...will evaluate as coin == (25 | coin) == (10 | coin) == 5
. Again, operator precedence.
What you want to do is this:
if coin in [25, 10, 5]:
or
if coin in (25, 10, 5):
or simply
if coin == 25 or coin == 10 or coin == 5:
Don't create problems and confusion for the next guy who reads your code for nothing. Simple and readable are your friends π