+ 4
Why the ans is just three?
num = 7 if num > 3: print("3") if num < 5: print("5") if num ==7: print("7") why not seven as well??
11 odpowiedzi
+ 18
the first "if" is correct, so it keep running the second one. the second is False, so it stop to run the third one. therfore, it only shows 3.
+ 8
let me help you to visualize what's happening here
num = 7
{if num >3:
print("3")
{if num<5:
print("5")
{if num ==7:
print("7")
}
}
}
this will output 3 (I added curly braces to help you understand) so as u can see the third if statement is inside the second if statement block so when the second if statement becomes false the entire block is ignored .
+ 4
hey the second if statement is false so it stops the execution right there and hence the only result is 3. see there is no "else" statement here so it stops after first false statement. if the was else statement then results would have been 3&7
+ 2
This is because the if statements are nested one inside the other. Remember that python group statements using indentation (the white space before each line). So, the second if will only be evaluated if the first one is true and the third one will only be evaluated if the second one is true.
In your code, as num is equal to 7, "if num < 5" will evaluate to false and everithing inside that if statement will never run (including the next if).
If you want this code to behave something similar as a Select-Switch statement, you can write it without the indentation like this:
num = 7
if num > 3:
print("3")
if num < 5:
print("5")
if num ==7:
print("7")
In this case, all the if statement will be checked and the final output will include 3 and 7.
Hope it helped.
+ 1
Because it's in the 'if num < 5' block. And 7<5 returns False.
+ 1
first "if" is correct, then it will continue and will run the second "if", but since it is false it stops running and will always print "3".
0
third if condition num==7 is inside ie nested in second if condition num<5 ...when first if is executed which is true then nested if (second condition ) is false . therefore third if which is nested inside false if is not executed
0
so, the first "if" is correct, so it keep running the second one. the second is False, so it stop to run the third one. therfore, it only shows 3.
0
its only output is three because the first argument is said to be true then the statement will stop .
- 1
this is a nested if statement it can be written like this
num = 7
if(num > 3) // condition true so if block is executed
{
print("3")
if(num < 5) // num is still 7 condition false
{
print("5")
if(num == 7)
{
print("7")
}
}
}
in second if the condition becomes false so that block won't be executed and the 3rd condition (num ==7) won't be executed because it is nested in the 2nd if block.
- 2
expected an indented block that mean that you didn't correct your spaces