shouldnt it be 7 because it says num==7 and num=7 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

shouldnt it be 7 because it says num==7 and num=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")

10th Apr 2017, 4:47 PM
taimur rahman
taimur rahman - avatar
5 Answers
+ 7
These are nested if blocks. So, if(num > 3) True Goes inside prints 3 if(num < 5) False Doesn't go inside. Hence none of the further code executed.
10th Apr 2017, 4:51 PM
Ashwani Kumar
Ashwani Kumar - avatar
+ 3
output is 3. because 1st condition is true only so "if" condition evaluate true and print it
10th Apr 2017, 5:43 PM
Vega
Vega - avatar
+ 1
It will only print out 3. The reason being when the code evaluates the line: if num < 5, it has evaluated false and everything inside the body will not be executed. So, the output will only be 3.
10th Apr 2017, 5:16 PM
Deddy Tandean
+ 1
Hello! here is my explanation: num = 7 <---This is the given value *if num > 3: <----This is the 1st condition, if it´s not satisfied then the program goes to condition #2 print("3") *if num < 5: <---- This is the second condition,if not satisfied goes to condition #3 print("5") *if num ==7: <----This is the third and final condition. print("7") As we can note, the number given is 7 and 7==7 right? Mathematically it is true, but the "If" condition in the program evaluates the input in a logical way as I explained above: Value given, if condition #1 is true the program ends, if not then evaluates condition #2, and so on. Because the first condition is TRUE (7 is greater than 3) then the program ends and the result is 3 because that is the instruction( print("3")). *P.D. In fact we will never get to the final condition (7==7) because the program would end immediately in the first condition. *P.D. Sorry if I do not explain myself clearly.
19th May 2017, 4:32 AM
Pedro Andreu
Pedro Andreu - avatar
0
What i understand is that it finds a false statement and stops the execution. I tryed to put the 7 statement before the 5, and it reads it
15th Apr 2017, 8:20 PM
Pedro Adrião
Pedro Adrião - avatar