+ 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
13 Respuestas
+ 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 .
+ 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
+ 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....!!!
+ 2
Use the right indentation:
num = 7
if num > 3:
print("3")
if num < 5:
print("5")
if num ==7:
print("7")
+ 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.
+ 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")
+ 1
Thanks to all
+ 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")
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?
0
We have already explained it in the top
0
Soham Mukherjee Because 7 < 5 is False, 5 nor 7 aren't printed. They key to understand that is to understand indentation.
0
if
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