+ 4

Why the ans is just three?

num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") why not seven as well??

17th Dec 2015, 5:02 AM
raj karnik
raj karnik - avatar
11 Réponses
+ 18
the first "if" is correct, so it keep running the second one. the second is False, so it stop to run the third one. therfore, it only shows 3.
17th Dec 2015, 6:34 PM
Lily Yang
Lily Yang - avatar
+ 8
let me help you to visualize what's happening here num = 7 {if num >3: print("3") {if num<5: print("5") {if num ==7: print("7") } } } this will output 3 (I added curly braces to help you understand) so as u can see the third if statement is inside the second if statement block so when the second if statement becomes false the entire block is ignored .
25th Oct 2016, 5:39 PM
Digbose Hazarika
Digbose Hazarika - avatar
+ 4
hey the second if statement is false so it stops the execution right there and hence the only result is 3. see there is no "else" statement here so it stops after first false statement. if the was else statement then results would have been 3&7
21st Dec 2015, 2:29 PM
shubham sharma
shubham sharma - avatar
+ 2
This is because the if statements are nested one inside the other. Remember that python group statements using indentation (the white space before each line). So, the second if will only be evaluated if the first one is true and the third one will only be evaluated if the second one is true. In your code, as num is equal to 7, "if num < 5" will evaluate to false and everithing inside that if statement will never run (including the next if). If you want this code to behave something similar as a Select-Switch statement, you can write it without the indentation like this: num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") In this case, all the if statement will be checked and the final output will include 3 and 7. Hope it helped.
6th Oct 2016, 11:37 AM
Nelson Urbina
Nelson Urbina - avatar
+ 1
Because it's in the 'if num < 5' block. And 7<5 returns False.
19th Nov 2016, 3:20 PM
donkeyhot
donkeyhot - avatar
+ 1
first "if" is correct, then it will continue and will run the second "if", but since it is false it stops running and will always print "3".
19th Nov 2016, 5:44 PM
Rajesh
Rajesh  - avatar
0
third if condition num==7 is inside ie nested in second if condition num<5 ...when first if is executed which is true then nested if (second condition ) is false . therefore third if which is nested inside false if is not executed
2nd Apr 2016, 9:15 AM
Nikhil Tyagi
Nikhil Tyagi - avatar
0
so, the first "if" is correct, so it keep running the second one. the second is False, so it stop to run the third one. therfore, it only shows 3.
30th Sep 2016, 1:09 AM
Chakib Dabbek
Chakib Dabbek - avatar
0
its only output is three because the first argument is said to be true then the statement will stop .
29th Dec 2016, 9:34 PM
Christian Moncada
Christian Moncada - avatar
- 1
this is a nested if statement it can be written like this num = 7 if(num > 3) // condition true so if block is executed { print("3") if(num < 5) // num is still 7 condition false { print("5") if(num == 7) { print("7") } } } in second if the condition becomes false so that block won't be executed and the 3rd condition (num ==7) won't be executed because it is nested in the 2nd if block.
3rd Jul 2016, 10:15 AM
Ankit Ak Singh Kharb
Ankit Ak Singh Kharb - avatar
- 2
expected an indented block that mean that you didn't correct your spaces
6th Sep 2016, 4:13 AM
Yassen Maged Youssef
Yassen Maged Youssef - avatar