Reputation: 199
In this code:
if raw_input("\n Enter 'y' or 'Y': ")==("y" or "Y"):
print("\n Success!")
It doesn't take the "OR" properly, instead if the in this case noncapital 'y' is entered the condition is fulfilled. If I enter the capital 'Y' I don't get the Success!
What's wrong here?
Upvotes: 1
Views: 450
Reputation: 9994
What's wrong here?
In many programming languages, OR
is a boolean operator. You apply it to values that are TRUE or FALSE. The operation evaluates to TRUE if at least one operand is TRUE:
TRUE OR TRUE == TRUE
TRUE OR FALSE == TRUE
FALSE OR TRUE == TRUE
FALSE OR FALSE == FALSE
In Python, you can apply or
on non-boolean operands:
x or y
returns x
if x
casted to boolean
is True
; else it returns y
. For boolean operands, this leads to the same results as above, but for non-boolean operands this has interesting effects:
[] or {}
is {}
(because empty lists are False
when casted to
boolean
)[1] or {}
is [1]
(because non-empty lists are True
when casted to boolean
)[1] or 1/0
is also [1]
(the right operand doesn't even get evaluated when the left one is True
, so we don't hit the ZeroDivisionError
. This is known as (left-to-right) short-circuit evaluation.)Thus, other than in natural language, the Python or
cannot be interpreted as separating alternative values. (Only alternative conditions / boolean expressions.)
There are several possibilities on how to make your code behave as expected:
The naive approach:
answer = raw_input("\n Enter 'y' or 'Y': ")
if answer == "y" or answer == "Y":
print("\n Success!")
Normalizing the input:
if raw_input("\n Enter 'y' or 'Y': ").lower() == 'y':
print("\n Success!")
Comparing to set of alternative values with membership operator in
:
if raw_input("\n Enter 'y' or 'Y': ") in {'y', 'Y'}:
print("\n Success!")
Upvotes: 0
Reputation: 1553
The right-hand-side of your if-statement is wrong and I think you need to understand a little better how the or
operator behaves between strings.
Keep in mind that the return value of or
is the value that has been evaluated last, and that Python evaluates empty string as boolean False
and non-empty strings as boolean True
.
In your case, the interpreter reads ("y" or "Y")
, it then evaluates the boolean value of "y" which is True
, as it is a non-empty string. Therefore, the boolean value of the or
statement it True
and the return value of the statement becomes "y", the last evaluated value.
This is how I would write this code. I would keep the return value of raw_input
in _input
, which will make it easier for me and others to read and understand the if-statement:
_input = raw_input("\n Enter 'y' or 'Y': ")
if input in ["y", "Y"]:
print("\n Success!")
Upvotes: 0
Reputation: 6500
Try to make a list of values and use the in
keyword. Something like this will work,
if raw_input("\n Enter 'y' or 'Y': ") in ('y', 'Y'):
print("\n Success!")
The in
keyword tests the string against a tuple of strings and on a correct match it returns True
.
Since here you have just one character, you can build a string "yY"
. Something like this will work,
if raw_input("\n Enter 'y' or 'Y': ") in "yY":
print("\n Success!")
Here each character of the string acts like one element of the tuple above.
ERROR in your code:
You used ("y" or "Y")
. This does not work in Python. This will only return "y"
as both "y"
and "Y"
are treated as True
values. However, if you type (0 or "Y")
, you will get "Y"
as 0
is treated as a False
value.
Upvotes: 1