I have this simple line of code:
i = " "
if i != "" or i != " ":
print("Something")
This should be simple, if i is not empty ""
OR it's not a space " "
, but it is, print Something. Now, why I see Something printed if one of those 2 conditions is False
?
De Morgan's laws,
In your case, as per the first statement, you have effectively written
which is not possible to occur. So whatever may be the input,
(i == "" and i == " ")
will always returnFalse
and negating it will giveTrue
always.Instead, you should have written it like this
or as per the quoted second statement from the De Morgan's law,
I will explain how
or
works.If checks the first condition and if it is true it does not even check for the second condition.
If the first condition is false only then it checks for second condition and if it is true the whole thing becomes true.
Because
So If you want to satisfy both condition of not empty and space use
and
Your print statement will always happen, because your logic statement is always going to be True.
if A or B:
will be True if either A is True OR B is True OR both are True. Because of the way you've written the statement, one of the two will always be True. More precisely, with your statement as written, the if statement correlates to
if True or False:
which simplifies toif True:
.It seems that you want an
and
statement instead of anor
.you have the condition as
here
i != ""
will evaluate toTrue
andi != " "
will evaluate toFalse
so you will have
True or False
=True
you can refer this truth table for
OR
hereThis condition:
will always be true. You probably want
and
instead ofor
...