if statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

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") I think its output should be 37.but It shows only 3.Can anyone explain it to Me

12th Jul 2019, 5:00 AM
Rgodella
Rgodella - avatar
13 Answers
+ 7
num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") just remove remove indentation num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") In python if you use 4 spaces it will take it as braces .
13th Jul 2019, 9:10 AM
Vijay(v-star🌟)
Vijay(v-star🌟) - avatar
+ 6
It is because of the identation. Yes it prints 3 on encountering the first if statement but the second if statement does not hold true it doesn't print 5. Next the third if statement is present inside the second if statement ,as the second if does not execute the third one also does not
13th Jul 2019, 7:29 AM
Manoj
Manoj - avatar
+ 2
Ur getting 3, Bcz of wrong indentation. Correct the indentation of all 3 if conditions, u will get 37..!! Python uses indentation (white space at the beginning of a line) to delimit blocks of code. Other languages, such as C, use curly braces to accomplish this, but in Python indentation is mandatory; programs won't work without it. Hope this helps....!!!
12th Jul 2019, 5:59 AM
Kuri
Kuri - avatar
+ 2
Use the right indentation: num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7")
12th Jul 2019, 8:08 AM
Farhad Biramvand
Farhad Biramvand - avatar
+ 2
This is right code: num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") Indentation is important thing in Python. You know that Python doesn't use curly braces {} at the begin of loop and ; at the end like C++/C. Here is Indentation priority.
13th Jul 2019, 11:00 PM
Baltazarus
Baltazarus - avatar
+ 1
#Because this comparison: if num < 5: #is false, #this indentation level, which belongs to the if statement is skipped. print("5") #This is not printed. if num == 7: #This is not tested. #This indentation level is also skipped. print("7")
12th Jul 2019, 1:56 PM
Seb TheS
Seb TheS - avatar
+ 1
Thanks to all
12th Jul 2019, 5:00 PM
Rgodella
Rgodella - avatar
+ 1
Thats nested if statement, thats meant the previous condition is need to be true or it Will not executed. In this case the second if statement condition is false //7<5=false , so the next if statement will not executed. You can fix with remove the space ,like this: num=7 if num>3: printf("3") if num<5: printf("5") if num ==7: printf("7")
13th Jul 2019, 12:51 PM
Early
Early - avatar
0
Shouldn't the answer here be 7, because it says in the code if num==7, print("7") right? But SoloLearn said the answer was 3. Please explain?
31st Jul 2019, 6:34 AM
Soham Mukherjee
Soham Mukherjee - avatar
0
We have already explained it in the top
31st Jul 2019, 7:41 AM
Early
Early - avatar
0
Soham Mukherjee Because 7 < 5 is False, 5 nor 7 aren't printed. They key to understand that is to understand indentation.
31st Jul 2019, 9:35 AM
Seb TheS
Seb TheS - avatar
0
if
2nd Sep 2019, 4:34 AM
Akash
0
What is the output of this code? num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") The ans is 3, because if num< 5: is false statement,and if num==7: is invalid statement.so the ans is 3
12th Sep 2020, 1:45 PM
Saravanan B
Saravanan B - avatar