0

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") Shouldn't the above code print both 3 and 7, not just 3?

24th Jun 2017, 6:32 PM
John Sitther
3 Antworten
+ 4
It does print both, you are correct.
24th Jun 2017, 6:43 PM
Rrestoring faith
Rrestoring faith - avatar
+ 6
That would be a good example to show how 'elif' works. If you put it like this: num = 7 if num > 3: print("3") elif num < 5: print("5") elif num == 7: print("7") only "3" will be printed out. This is because 'elif' is executed *only* when the 'if' statement is False. When its True, 'elif' is omitted.
24th Jun 2017, 7:37 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
No, because of the middle "if" "if num < 5: print("5")" statement which is false and the code is stopped running here as no "else/elif" statement are present.
5th Jul 2017, 6:52 PM
Nicolae Grati
Nicolae Grati - avatar