In: Computer Science
Write a program in Python to practice evaluating some logical expressions and determine whether the expressions are equivalent.
(( A ⋁ ¬C) ⋀ D)
in your code.
(( ¬A) ⊕ B)
in your code. HINT: There no Boolean ⊕ in Python. How would you write the code for the ⊕ operator using if statements and the 3 standard Boolean operators: AND, OR, and NOT?
Otherwise print a message saying “untrue.” At this stage, your program will produce just one line of output per run.
Change your program so instead of asking the user to enter an A, B, C, and D value, the computer uses loops to iterate through all the possible combinations of A, B, C, and D values. HINT: What possible values can A take on? What possible values can B take on? Nested loops will be your friend here.
For each set of A, B, C, and D values, print these input values on one line along with the computed match/untrue message for that line.
Example: (( A ⋁ ¬C) ⋀ D)
Input express 1 express 2 result
0 0 0 0 0 1 untrue
0 0 0 1 1 1 matches
…..
Here is the Python code:
A = int (input ("Input A, either 0 or 1:"))
B = int (input ("Input B, either 0 or 1:"))
C = int (input ("Input C, either 0 or 1:"))
D = int (input ("Input D, either 0 or 1:"))
A = False if A == 0 else True
B = False if B == 0 else True
C = False if C == 0 else True
D = False if D == 0 else True
def firstexpr (A, C, D):
return ((A or (not C)) and D)
Expr1 = firstexpr (A, C, D)
def xor (firstbool, secondbool):
return (firstbool != secondbool)
Expr2 = xor (not A, B)
if (Expr1 == Expr2):
print ("Matches")
else:
print ("Untrue")
Equivalent = True
for a in False, True:
for b in False, True:
for c in False, True:
for d in False, True:
lhs = int (firstexpr (a, c, d))
rhs = int (xor (a, b))
if lhs != rhs:
Equivalent = False
comparison = "Matches" if lhs == rhs else "Untrue"
print (int (a), int (b), int (c), int (d), "\t", lhs, rhs, comparison )
if Equivalent:
print ("Equivalent")
else:
print ("Not equivalent")