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

if statement

What is the output of this code? num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") sir, why the answer wont be 7?

15th Apr 2020, 9:52 AM
PRIYANKA K
PRIYANKA K - avatar
10 Answers
+ 7
Because 7 is not less than 5. So it will not go inside nested if part
15th Apr 2020, 9:55 AM
A͢J
A͢J - avatar
+ 4
Because the number isn't smaller than 5 only the first if statement will be executed. The output will be 3.
15th Apr 2020, 9:56 AM
Jannik Müller
Jannik Müller - avatar
+ 4
I guess what you wanted to do is: num = 7 if num > 3: print(3) if num < 5: print(5) if num == 7: print(7)
15th Apr 2020, 9:57 AM
Jannik Müller
Jannik Müller - avatar
+ 3
This exact question was asked a week ago. Read through this post for the answer in full https://www.sololearn.com/Discuss/2232416/?ref=app
15th Apr 2020, 10:04 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
it's matter of blocks if num >3: # True print("3") # 3 printed if num < 5: # False print("5") # here python go out of this block and don't excute any thing inside this block # notice that if num == 7 is inside the False block. that mean python will not print 5 and will not verify if num == 7 finally don't print 7
18th Apr 2020, 1:59 AM
Zouhair Mocho
Zouhair Mocho - avatar
+ 1
Because of the indentation, the code stop running after completing the first query. if num >3: #True print('3') #3 If you were to remove the indentation, then the code will examine each query for a result of 3 7
15th Apr 2020, 9:59 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Thank you everyone for your clarification
15th Apr 2020, 2:14 PM
PRIYANKA K
PRIYANKA K - avatar
+ 1
This question is similar PRIYANKA K. It won't print 7 because the second statement is false therefore leaving the third statement void.
16th Apr 2020, 9:52 AM
Aaron Luke Olvida🇵🇭
Aaron Luke Olvida🇵🇭 - avatar
0
the third if statement actullary has == not ==. so it will be if num === 7. I think
16th Apr 2020, 3:43 AM
Sali65
Sali65 - avatar
0
as num=7, so num<5 won't hold and only the first segment of the code will be executed.
4th Oct 2020, 11:49 AM
Suhani Goyal
Suhani Goyal - avatar