@virnuls/

Logic Gate

Python

One of the OCR Coding Challenges

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
def ask(question,acceptable):
  answer = None
  while answer not in acceptable:
    answer = input(question).upper()
  return answer

operator = {"AND":"and","OR":"or","XOR":"^","EOR":"^","NAND":"","NOR":"","NXOR":"","NEOR":""}
gate = ask("Enter logic gate: ", list(operator))
a = ask("Enter first input: ",("0","1")) + " "
b = " " + ask("Enter first input: ",("0","1"))
if gate[0] == "N":
  print(a+gate+b,"=",int(not(eval(a+operator[gate[1:]]+b))))
else:
  print(a+gate+b,"=",int(eval(a+operator[gate]+b)))