-3

so I was just looking over a few problems where I had to interpret a piece of code, and I stumbled upon this one :

a = 10
b = 4
c = 2
d = 3
if ((c+2<d)) or ((c*d)==(a-b)):
    if (True and not(True)) or True:
        print ("X")
    else:
        print("Y")
    print("Z")

I understand that the first part of the statement will evaluate to False, while the second part will evaluate to True. The problem I am having is about interpreting the if statement that follows. What is the "True and False" or "True" referring to, the previous statement? Thanks

1 Answers1

0

First, you need a closing parenthesis after that last True.

Second, that second if statement will always evaluate to True, because of the or True part. It's not referring to anything. It's just using the Python built-in constant True.

Here's a corrected version of that code. It runs, but it doesn't accomplish anything significant. It will always print "X" and "Z".

a = 10
b = 4
c = 2
d = 3
if ((c+2<d)) or ((c*d)==(a-b)):
    if (True and not(True) or True):
        print ("X")
    else:
        print("Y")
    print("Z")
Troy Gizzi
  • 2,480
  • 1
  • 14
  • 15
  • Sorry, I forgot to add the parenthesis, fixed that. So if i understand this properly, the second if statement is saying if "True and False" or "True". The first part will evaluate to False and the second will be True, and since there is an "or", its either False or True? – Vikram Padem Oct 18 '14 at 20:12