If else question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

If else question

X=True Y=False Z=False if X or Y and Z: print ('yes') else: print ('no') Isn't this expression supposed to print 'no'?

2nd Oct 2019, 2:56 PM
Onimisi
Onimisi - avatar
9 Answers
+ 1
Becuase x is true the rest of the exspression is ignored and prints yes
2nd Oct 2019, 3:04 PM
D_Stark
D_Stark - avatar
+ 1
I think and has a higher priority than or so Y and Z evaluates first resulting in False then X or false results in True.
2nd Oct 2019, 3:05 PM
xyz$
+ 1
You got indentation error for print Code: - If X or Y and Z=='False': print('yes') Else: print('no')
3rd Oct 2019, 7:10 PM
AJAY LINGAYAT
AJAY LINGAYAT - avatar
0
'or' operator means any of the one if is True then will print 'yes'.
2nd Oct 2019, 3:07 PM
★«D.Connect_Zone»
★«D.Connect_Zone» - avatar
0
Ok thanks
2nd Oct 2019, 3:10 PM
Onimisi
Onimisi - avatar
0
It prints yes because and has higher precedence order than or. You can use parentheses to control operation precedences.
2nd Oct 2019, 3:16 PM
Seb TheS
Seb TheS - avatar
0
priority order is NOT AND OR use this Y AND Z = FALSE THEN X OR FALSE = TRUE SO answer is TRUE
2nd Oct 2019, 3:19 PM
Saurav Sharma
Saurav Sharma - avatar
0
Priority order- NOT>AND> OR Therefore; If X OR Y AND Z = IF X OR (Y AND Z) So you will solve brackets first. This implies, Y AND Z= FALSE AND FALSE = FALSE NOW X OR FALSE = TRUE OR FALSE = TRUE THUS , this code will print 'yes'.
3rd Oct 2019, 6:30 AM
namrata garg
namrata garg - avatar
0
Yes because you used 'or' statement so one condition is required to be true. If X 'or' y and z For detail these is Logical operators Or And Not
3rd Oct 2019, 4:15 PM
Parampreet Rai
Parampreet Rai - avatar