#Defining AND circuit def AND(a, b): #If both values are 1 return 1 if a == 1 and b == 1: return 1 else: #Otherwise return 0 return 0 #Defining OR circuit def OR(a, b): #If either of value is 1 return 1 if a + b == 1 or a == b == 1: return 1 else: #Otherwise return 0 return 0 #Defining NOT circuit def NOT(a): #If the input is 1 if a == 1: #Return 0 return 0 #If the input is 0 elif a == 0: #Return 1 return 1 #Defining NAND circuit def NAND(a, b): a = AND(a, b) if a == 1: return 0 else: return 1 #Defining NOR circuit def NOR(a, b): a = OR(a, b) if a == 1: return 0 else: return 1 #Defining XOR circuit def XOR(a, b): a = OR(AND(a, NOT(b)), AND(NOT(a), b)) if a == 1: return a else: return 0 #Defining NXOR circuit def NXOR(a, b): a = XOR(a, b) if a == 1: return 0 elif a == 0: return 1 #Defining eight() def eight(a, b, c): # (A ∨ B) ꚛ C = Q a = XOR(OR(a, b), c) #Return a return a #Defining main() if __name__ == '__main__': #Printing AND gate print('Truth Table --- AND GATE') print('|\tA\t|\tB\t| Output') print('|\t0\t|\t0\t| ', AND(0, 0)) print('|\t0\t|\t1\t| ', AND(0, 1)) print('|\t1\t|\t0\t| ', AND(1, 0)) print('|\t1\t|\t1\t| ', AND(1, 1)) print() print() #Printing OR gate print('Truth Table --- OR GATE') print('|\tA\t|\tB\t| Output') print('|\t0\t|\t0\t| ', OR(0, 0)) print('|\t0\t|\t1\t| ', OR(0, 1)) print('|\t1\t|\t0\t| ', OR(1, 0)) print('|\t1\t|\t1\t| ', OR(1, 1)) print() print() #Printing NOT gate print('Truth Table --- NOT GATE') print('|\tA\t| Output') print('|\t0\t| ', NOT(0)) print('|\t1\t| ', NOT(1)) print() print() #Printing NAND gate print('Truth Table --- NAND GATE') print('|\tA\t|\tB\t| Output') print('|\t0\t|\t0\t| ', NAND(0, 0)) print('|\t0\t|\t1\t| ', NAND(0, 1)) print('|\t1\t|\t0\t| ', NAND(1, 0)) print('|\t1\t|\t1\t| ', NAND(1, 1)) print() print() #Printing NOR gate print('Truth Table --- NOR GATE') print('|\tA\t|\tB\t| Output') print('|\t0\t|\t0\t| ', NOR(0, 0)) print('|\t0\t|\t1\t| ', NOR(0, 1)) print('|\t1\t|\t0\t| ', NOR(1, 0)) print('|\t1\t|\t1\t| ', NOR(1, 1)) print() print() #Printing XOR gate print('Truth Table --- XOR GATE') print('|\tA\t|\tB\t| Output') print('|\t0\t|\t0\t| ', XOR(0, 0)) print('|\t0\t|\t1\t| ', XOR(0, 1)) print('|\t1\t|\t0\t| ', XOR(1, 0)) print('|\t1\t|\t1\t| ', XOR(1, 1)) print() print() #Printing NXOR gate print('Truth Table --- NXOR GATE') print('|\tA\t|\tB\t| Output') print('|\t0\t|\t0\t| ', NXOR(0, 0)) print('|\t0\t|\t1\t| ', NXOR(0, 1)) print('|\t1\t|\t0\t| ', NXOR(1, 0)) print('|\t1\t|\t1\t| ', NXOR(1, 1)) print() print() #Printing (A ∨ B) ꚛ C = Q print('Truth Table --- NXOR GATE') print('|\tA\t|\tB\t| Output') print('|\t0\t|\t0\t| ', NXOR(0, 0)) print('|\t0\t|\t1\t| ', NXOR(0, 1)) print('|\t1\t|\t0\t| ', NXOR(1, 0)) print('|\t1\t|\t1\t| ', NXOR(1, 1)) #Printing (A ∨ B) ꚛ C = Q gate (NXOR for 3 value inputs) print('Truth Table for (A ∨ B) ꚛ C = Q') print('|\tA\t|\tB\t|\tC\t| Output') print('|\t0\t|\t0\t|\t0\t|', eight(0, 0, 0)) print('|\t0\t|\t0\t|\t1\t|', eight(0, 0, 1)) print('|\t0\t|\t1\t|\t0\t|', eight(0, 1, 0)) print('|\t0\t|\t1\t|\t1\t|', eight(0, 1, 1)) print('|\t1\t|\t0\t|\t0\t|', eight(1, 0, 0)) print('|\t1\t|\t0\t|\t1\t|', eight(1, 0, 1)) print('|\t1\t|\t1\t|\t0\t|', eight(1, 1, 0)) print('|\t1\t|\t1\t|\t1\t|', eight(1, 1, 1))
Can provide a description outlining which option chosen and a description of how the code works and how to use it.
Also can you fix the code.
#Defining AND circuit
def AND(a, b):
#If both values are 1 return 1
if a == 1 and b == 1:
return 1
else:
#Otherwise return 0
return 0
#Defining OR circuit
def OR(a, b):
#If either of value is 1 return 1
if a + b == 1 or a == b == 1:
return 1
else:
#Otherwise return 0
return 0
#Defining NOT circuit
def NOT(a):
#If the input is 1
if a == 1:
#Return 0
return 0
#If the input is 0
elif a == 0:
#Return 1
return 1
#Defining NAND circuit
def NAND(a, b):
a = AND(a, b)
if a == 1:
return 0
else:
return 1
#Defining NOR circuit
def NOR(a, b):
a = OR(a, b)
if a == 1:
return 0
else:
return 1
#Defining XOR circuit
def XOR(a, b):
a = OR(AND(a, NOT(b)), AND(NOT(a), b))
if a == 1:
return a
else:
return 0
#Defining NXOR circuit
def NXOR(a, b):
a = XOR(a, b)
if a == 1:
return 0
elif a == 0:
return 1
#Defining eight()
def eight(a, b, c):
# (A ∨ B) ꚛ C = Q
a = XOR(OR(a, b), c)
#Return a
return a
#Defining main()
if __name__ == '__main__':
#Printing AND gate
print('Truth Table --- AND GATE')
print('|\tA\t|\tB\t| Output')
print('|\t0\t|\t0\t| ', AND(0, 0))
print('|\t0\t|\t1\t| ', AND(0, 1))
print('|\t1\t|\t0\t| ', AND(1, 0))
print('|\t1\t|\t1\t| ', AND(1, 1))
print()
print()
#Printing OR gate
print('Truth Table --- OR GATE')
print('|\tA\t|\tB\t| Output')
print('|\t0\t|\t0\t| ', OR(0, 0))
print('|\t0\t|\t1\t| ', OR(0, 1))
print('|\t1\t|\t0\t| ', OR(1, 0))
print('|\t1\t|\t1\t| ', OR(1, 1))
print()
print()
#Printing NOT gate
print('Truth Table --- NOT GATE')
print('|\tA\t| Output')
print('|\t0\t| ', NOT(0))
print('|\t1\t| ', NOT(1))
print()
print()
#Printing NAND gate
print('Truth Table --- NAND GATE')
print('|\tA\t|\tB\t| Output')
print('|\t0\t|\t0\t| ', NAND(0, 0))
print('|\t0\t|\t1\t| ', NAND(0, 1))
print('|\t1\t|\t0\t| ', NAND(1, 0))
print('|\t1\t|\t1\t| ', NAND(1, 1))
print()
print()
#Printing NOR gate
print('Truth Table --- NOR GATE')
print('|\tA\t|\tB\t| Output')
print('|\t0\t|\t0\t| ', NOR(0, 0))
print('|\t0\t|\t1\t| ', NOR(0, 1))
print('|\t1\t|\t0\t| ', NOR(1, 0))
print('|\t1\t|\t1\t| ', NOR(1, 1))
print()
print()
#Printing XOR gate
print('Truth Table --- XOR GATE')
print('|\tA\t|\tB\t| Output')
print('|\t0\t|\t0\t| ', XOR(0, 0))
print('|\t0\t|\t1\t| ', XOR(0, 1))
print('|\t1\t|\t0\t| ', XOR(1, 0))
print('|\t1\t|\t1\t| ', XOR(1, 1))
print()
print()
#Printing NXOR gate
print('Truth Table --- NXOR GATE')
print('|\tA\t|\tB\t| Output')
print('|\t0\t|\t0\t| ', NXOR(0, 0))
print('|\t0\t|\t1\t| ', NXOR(0, 1))
print('|\t1\t|\t0\t| ', NXOR(1, 0))
print('|\t1\t|\t1\t| ', NXOR(1, 1))
print()
print()
#Printing (A ∨ B) ꚛ C = Q
print('Truth Table --- NXOR GATE')
print('|\tA\t|\tB\t| Output')
print('|\t0\t|\t0\t| ', NXOR(0, 0))
print('|\t0\t|\t1\t| ', NXOR(0, 1))
print('|\t1\t|\t0\t| ', NXOR(1, 0))
print('|\t1\t|\t1\t| ', NXOR(1, 1))
#Printing (A ∨ B) ꚛ C = Q gate (NXOR for 3 value inputs)
print('Truth Table for (A ∨ B) ꚛ C = Q')
print('|\tA\t|\tB\t|\tC\t| Output')
print('|\t0\t|\t0\t|\t0\t|', eight(0, 0, 0))
print('|\t0\t|\t0\t|\t1\t|', eight(0, 0, 1))
print('|\t0\t|\t1\t|\t0\t|', eight(0, 1, 0))
print('|\t0\t|\t1\t|\t1\t|', eight(0, 1, 1))
print('|\t1\t|\t0\t|\t0\t|', eight(1, 0, 0))
print('|\t1\t|\t0\t|\t1\t|', eight(1, 0, 1))
print('|\t1\t|\t1\t|\t0\t|', eight(1, 1, 0))
print('|\t1\t|\t1\t|\t1\t|', eight(1, 1, 1))
Step by step
Solved in 3 steps with 2 images