0
Why A==(A or B) is True, while B==(A or B) is False?
I have very simple program to write, which check if input is A or B. I know I could go for: if input() == A or input == B: print("Ok") But why you can't make something like this? if input() == (A or B): print("Ok") In this scenario A == (A or B) is True B == (B or A) is True But A == (B or A) is False B == (A or B) is False Theoretically it's true when True==(False or True) changing it to numbers (1=0+1) Is it because strings don't have logic value of 1 prior to checking it with == ? Why would then A==(A or B) work? I'm bit confused.
4 odpowiedzi
+ 4
All you need to know how or operator works.
or returns the first Truthy value if there are any, else return the last value in the expression.
print( x or y)
It means if x is a Truthy value x is returned, y vice versa.
Now, you can think about that cases.
+ 3
You need to store the value of input & then compare it separately.
if input() == ('A' or 'B') mean if input() == 'A' or 'B' so, here we didn't actually defined any clear logic for the second case.
+ 2
Piotr Ś
We have to check the equality of B just like we did with the A.
x = input()
if x == 'A' or x == 'B':
print('ok')
+ 1
Ah ok, so statement after "or" is "whatever, we already got our True, don't bother with mess after "or" "?