0
in nested 'if' . why dos 'if' statement is execute the first answer, even there is some right answer in below. why is work in first come first serve, if I wanted to get more answer in an 'if' state ment is it possible in python
2 Respostas
+ 2
You're asking about this problem:
num=7
if num > 3:
print("3")
if num < 5:
print("5")
if num ==7:
print("7")
Answer: Because num is not less than 5; the test for 7 can actually never be true.
In separate "if" statements, this logic is:
if num > 3:
print("3")
if (num > 3) and (num < 5): # You must include the context from the previous "if"
print("5")
if (num > 3) and (num < 5) and (num == 7):
print("7")
It is impossible for the third "if" statement to ever be true (you could call this a bug).
0
The thing to understand is that Python is lazy : in an 'if' statement, if the condition evaluated to True, then Python will NEVER go further.