+ 2

Wondering where the problem of my code is...

I am new on coding,and just started python. Just learned if and else statements. my code in question is... x = 7 if x == 7: if x > 4: if x < 2: print ("yes") else: print("no") since the third statement is false,Shouldn’t it print ‘no’ ? I do not get any output after running this code. however if i run it without the third statement,i get ‘Yes’

22nd Apr 2021, 2:59 PM
Ahnaf Inan
Ahnaf Inan - avatar
3 Answers
+ 1
Ahnaf Inan Your "else" block is only applicable for the first "if" statement here, and as it's True, the "else" block isn't executed. Try this code: x = 7 if x == 7 and x > 4 and x < 2: print("yes") else: print("no") The result is always "no" as no number can be greater than 4 and less than 2, simultaneously.
22nd Apr 2021, 3:14 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Calvin Thomas Thank you very much. I got it :)
22nd Apr 2021, 5:57 PM
Ahnaf Inan
Ahnaf Inan - avatar
0
Nikhil Thanks a lot for the explanation :D
22nd Apr 2021, 5:58 PM
Ahnaf Inan
Ahnaf Inan - avatar