Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
' | ' Operator and 'OR'
#9
Okay, I believe I see the issue and it appears that nilamo was right. From what I'm seeing in your tests, is_divisible() should be True when n is evenly divisible by both x and y. In which case, the logic should be:

def is_divisible(n, x, y):
    return n % x == 0 and n % y == 0
Also, I noticed something odd. The bitwise statement you had was returning False for (8, 3, 4), which is the correct result according to the test you provided: Test.assert_equals(is_divisible(8,3,4),False). Going through the steps, that could only happen if the bitwise operator was evaluated prior to the equality test (==0).

Quote:8 % 3 evaluates to 2.
8 % 4 evaluates to 0.
2 | 0 evaluates to 2.
2 == 0 evaluates to False.

However, equality has higher precedence than the bitwise operator per 6.16 of the Python Language Reference. So, it should be evaluated as:

Quote:8 % 3 evaluates to 2.
8 % 4 == 0 evaluates to True.
2 | True evaluates to 3 (True).

I'm not sure what to make of that.



And I found my answer in 6.10 of the Python Language Reference:

Quote:Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation.
Reply


Messages In This Thread
' | ' Operator and 'OR' - by blackknite - Jan-10-2019, 01:10 AM
RE: ' | ' Operator and 'OR' - by stullis - Jan-10-2019, 02:24 AM
RE: ' | ' Operator and 'OR' - by blackknite - Jan-10-2019, 04:47 PM
RE: ' | ' Operator and 'OR' - by stullis - Jan-10-2019, 06:28 PM
RE: ' | ' Operator and 'OR' - by nilamo - Jan-10-2019, 06:40 PM
RE: ' | ' Operator and 'OR' - by Gribouillis - Jan-10-2019, 06:54 PM
RE: ' | ' Operator and 'OR' - by nilamo - Jan-10-2019, 07:08 PM
RE: ' | ' Operator and 'OR' - by blackknite - Jan-10-2019, 11:08 PM
RE: ' | ' Operator and 'OR' - by stullis - Jan-11-2019, 01:23 AM
RE: ' | ' Operator and 'OR' - by nilamo - Jan-11-2019, 05:51 AM
RE: ' | ' Operator and 'OR' - by blackknite - Jan-11-2019, 09:32 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020