Write a program that will accept any boolean expression in two variables and wil
ID: 2971005 • Letter: W
Question
Write a program that will accept any boolean expression in two variables and will print out the truth table for that expression. Use || for or, && for and, and ! for not. Use -> for implication and <-> for biconditional.
For example, if the expression was:
!(p && q) <-> ( !p || !q)
Your program would print:
p q | !(p && q) <-> (!p || !q)
-------------------------------
T T | F T T T T F F F
T F | T T F F T F T T
F T | T F F T T T T F
F F | T F F F T T T T
Explanation / Answer
# Define a few variables
>>> x = 10
>>> y = 12
>>> z = 14
# (y*z) is evaluated first, then x is added
>>> x + y * z
178
# (x * y) is evaluated first, then z is subtracted from the result
>>> x * y - z
106
# When chaining comparisons, a logical 'and' is implied. In this
# case, x < y and y <= z and z > x
>>> x < y <= z > x
True
# (2 * 0) is evaluated first and since it is False or zero, it is returned
>>> 2 * 0 and 5 + 1
0
# (2 * 1) is evaluated first, and since it is True or not zero, the (5 + 1) is evaluated and
# returned
>>> 2 * 1 and 5 + 1
6
# x is returned if it is True, otherwise y is returned if it is False. If neither
# of those two conditions occur, then z is returned.
>>> x or (y and z)
10
# In this example, the (7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.