Conditionals and Flow Control (Codecademy Python 9/15)

1

Hello, I'm doing a codecademy exercise and I have to solve the following conditional ones:

  • Equal bool_one to False result or not True and True
  • Equal bool_two to False result and not True or True
  • Equals bool_three to the result of True and not (False or False)
  • Equal bool_four to the result of not True or False and not True
  • Equal bool_five to the result of False or not (True and True)

Here are my answers:

bool_one = False
bool_two = False
bool_three = True
bool_four = True
bool_five = False

But the validator of the exercise is indicating that one or more answers are wrong.

    
asked by anonymous 25.09.2015 / 14:03

1 answer

4

Your error is in the second item, which is this:

  • Equal bool_two to False result and not True or True

Running the code:

print False and not True or True

You will have as a result:

  

True

Looking at the precedence table in Python Docs - Expressions you will see that not has higher precedence among the operators of this expression, and and has the second highest precedence, being executed before or but after not . Therefore the compiler does the following calculations:

  • not True = > False
  • False and False = > False
  • False or True = > True (end result)

When making a testo and giving greater precedence to or you can see that the result is False . Example:

print False and not (True or True)

Result:

  

False

    
25.09.2015 / 14:38