0

Wrong answer?

So I came across this question and it states the answer is 3. Wouldn't it be 3 and 7? Is question was found on if statements. Question 3/3. ---- What is the output of this code? num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") ----

18th Dec 2018, 5:30 PM
Effoffwillyageez
Effoffwillyageez - avatar
4 Réponses
+ 3
Effoffwillyageez The point is that "if num == 7" is more indented than "if num < 5". The condition num < 5 is false, so it won't even check if num == 7. Everything that is on the same indentation level as "if num < 5" (or even further indented) won't be executed because the initial condition num < 5 is false. Anything after line 4 won't be executed.
18th Dec 2018, 6:42 PM
Anna
Anna - avatar
+ 1
the answer is 3 because the condition was met on the first if statement and the next nested was false. so it stopped
18th Dec 2018, 5:44 PM
Zakariya Abubakar
Zakariya Abubakar - avatar
0
Oh. So if the condition is met before the rest of the code inside the nest is executed then it doesn't need to execute the rest?
18th Dec 2018, 5:49 PM
Effoffwillyageez
Effoffwillyageez - avatar
0
python compiler executes line by line so output will be 3 when first condition faiks then it jumps to next one hope you are cleared
18th Dec 2018, 5:56 PM
Mritunjay Pandey
Mritunjay Pandey - avatar