Why is 3 the only answer???? why isn't answer 37 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is 3 the only answer???? why isn't answer 37

What is the output of this code? num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") Why isnt the answer? 3 7 Oder of operations kind of thing??

21st Jun 2020, 6:03 PM
BJ Bubala
BJ Bubala - avatar
3 Answers
+ 5
This question catches sooo many people out. The statement if num == 7: is inside the code block of the statement if num < 5: - since that if statement is False, that code block doesn't get executed. This means the num == 7 doesn't even get parsed.
21st Jun 2020, 6:10 PM
Russ
Russ - avatar
+ 4
BJ Bubala Because 7 is greater than 3 but not less than 5 so it will print 3 only. To print 7 , first condition 7 < 5 must be satisfied. It is a nexted if condition. To satisfy inner if condition first outer if condition must be satisfied. so here num == 7 is a inner if condition of outer if condition num < 5 And num < 5 is a inner if condition of outer if condition num > 3
21st Jun 2020, 7:17 PM
A͢J
A͢J - avatar
+ 2
Hey BJ Bubala You Getting Only 3 Because In condition first : 7 is greater than 3 so it already printed ‘3’ for you but in condition second 7 is not less than 5 so condition gone false and it comes out.
21st Jun 2020, 6:19 PM
Rahul Saxena
Rahul Saxena - avatar