0

Control Structure: Boolean Logic

I am still learning. When I took this section, i don't understand why the answer is 2. What is the result of this code? if not True: print("1") elif not (1 + 1 == 3): print("2") else: print("3") Can someone explain to me why the answer is "2" and not "1 "or "3"? Also, why does the code start with "if not true:" since I usually understand it if it would have said "if not 1==1"

23rd Jun 2020, 10:11 PM
Ken Phan
Ken Phan - avatar
2 Respostas
+ 6
If statement is evaluated only if its condition is True. The first condition is not met, as "not True" is False. Then we check the second condition - "not (1+1==3)". Since 1+1==3 returns False, the "not False" is of course True. So the second condition is evaluated -- it prints("2"). Because we found a True condition of the if statement, we automatically stop checking all others. So the "else" part is ignored (as would have been any other "elif" after the "2", if it was there).
23rd Jun 2020, 10:17 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
Thanks a ton!
23rd Jun 2020, 10:19 PM
Ken Phan
Ken Phan - avatar