0

Why does this IF statement come out as 3 and not 7?

What is the output of this code? num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") If I am understanding correctly, when you type in 7==7 ict omes out as true but the answer was 3? Is it going in order of what is true first or something?

20th Feb 2020, 1:07 PM
Brandon
4 ответов
+ 2
Only 3 , You are right but code syntax is wrong num = 7 if num > 3: print("3") if num < 5:#under first if body print("5") if num ==7:#under 2nd if body and second if is wrong print("7") Correct code is : num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7")
20th Feb 2020, 1:14 PM
Prathvi
Prathvi - avatar
+ 2
7 is greater than 3 so 3 get printed then it moves to 7 is less than 5 which is false, since the next statement is inside the num < 5 part it get out without going further, it only will check num ==7 if num is < 7
20th Feb 2020, 1:15 PM
✳AsterisK✳
✳AsterisK✳ - avatar
0
If statement is like any other code. Statement: if num == 7: ... was not ran because it was nested in: if num < 5: ... which was false.
20th Feb 2020, 1:13 PM
Seb TheS
Seb TheS - avatar
0
ah okay, thanks everyone so it was executing it from top to bottom. Makes sense. Thank you !
20th Feb 2020, 1:18 PM
Brandon