Building logical gates

I am trying to create a program that will help solve a complex logic gate scheme. To do this, I tried to build the main logical gate and checked the test for them:

def isbit(a):
    if(a==0 | a==1) : return True
    return False
  / # creation Not,Nor,Nand,Or(|),And(&),Xor(^) even tho by using bitwise     operators that exsit in phyton.
def Nor(a,b):
    assert(isbit(a) & isbit(b)) ,"inputs to Nor are not Bit Type" # asserst     is equal to  raise - if - not
    return not(a|b)

def Or(a,b):
    assert(isbit(a) & isbit(b)) ,"inputs to or are not Bit Type" # asserst     is equal to  raise - if - not
    return (a|b)

def Xor(a,b):
    assert(isbit(a) & isbit(b)) ,"inputs to or are not Bit Type" # asserst     is equal to  raise - if - not
    return (a^b)

def And(a,b):
    assert(isbit(a) & isbit(b)) ,"inputs to or are not Bit Type" # asserst     is equal to  raise - if - not
   return (a&b)

def Nand(a,b):
    assert(isbit(a) & isbit(b)) ,"inputs to or are not Bit Type" # asserst is equal to  raise - if - not
   return not(And(a,b))

def Not(a):
   assert(isbit(a)) ,"inputs to or are not Bit Type" # asserst is equal to      raise - if not
    return not(a)


def main():
   pass

 if __name__ == '__main__':
    main()


x=1
y=1
print Xor(Nor(And(x,y),Nand(x,y)),Or(And(x,y),Nand(x,y)))

the script returns:

Message File Name   Line    Position    
Traceback               
    <module>    <module1>   51      
    Nor <module1>   18      
AssertionError: inputs to Nor are not Bit Type              

I do not understand why this raises my statement if I only send 1s input functions .

+4
source share
1 answer

Note that:

if (a == 0 | a == 1):

due to Python , operator precedence is evaluated as:

if a == (0 | a) == 1:

(Python is not C, you do not always need parentheses after if), which can only be true if a == 1. Instead, if you decide to use bitwise operators everywhere, you should write:

if (a == 0) | (a == 1):

In my opinion, simply:

return a in {0, 1} 

. return True , if, Boolean.

+4

All Articles