Comparator vs. arithmetic order of operations


Did you add a False after each else?

I expect the expression return 4**0 != 1 to return false but it seems not to do so. Does this mean that comparators are evaluated before arithmetic operations?

answer = "'Tis but a scratch!"

def black_knight():
    if answer == "'Tis but a scratch!":
        return True
        return not True       # Make sure this returns False

def french_soldier():
    if answer == "Go away, or I shall taunt you a second time!":
        return True
        return 4**0 != 1       # Make sure this returns False


I think there may be something wrong about how you tested that.

$ python
>>> 4**0 != 1


A Python Operator precedence table may be helpful.

The table linked above lists Python 3 operator precedence from lowest to highest precedence, so operations specified at the bottom are performed first, while ones at the top are last.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.