Need a small clarification on the following code in the description... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Need a small clarification on the following code in the description...

if 1 == 3: print ("1") elif 1 == 3 and 5: print ("2") elif 1 == 4 or 1 == 7: print ("3") elif 1 == 3 or 2: print ("3a") elif 1 == 1: print ("4") else : print ("5") # Why is my output ==> 3a instead of ==> 4 ? Why is the condition [ 1 == 3 or 2 ] True as I think it means if 1 is equal to either 2 or 3 or both ( and this should be False)

14th Aug 2017, 2:41 PM
ARC
ARC - avatar
2 Answers
+ 1
it evaluated the whole expression as true because of the second part: or 2 this interprets as: is 2 a truthy value ? which is true what you should do is write it as follows: elif 1== 3 or 1 == 2
14th Aug 2017, 2:56 PM
Medhat Youssef
Medhat Youssef - avatar
+ 1
also the expression and 5 translates to: and 5 is a truthy value? which is also true .. but the whole expression successfully translated to false because of the first part: 1 == 3
14th Aug 2017, 2:58 PM
Medhat Youssef
Medhat Youssef - avatar