Print the truth table of a formula in p and q.
The formula is written in Python: and, or, not, and brackets. For the other connectives, Python's comparisons of True and False do the job: p != q is exclusive or, p == q is if-and-only-if, and p <= q is implication (False only when p is True and q is False, just like p → q).
Input
One line: the formula.
Output
Four lines, for the rows p, q = T T, T F, F T, F F in that order. Each line has the values of p, q and the formula, as T or F, separated by spaces.
Examples
p and q
T T T T F F F T F F F F
p <= q
T T T T F F F T T F F T
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Two nested loops over (True, False) give the rows in the right order.
Hint 2
eval(formula, {"p": p, "q": q}) works out the formula for one row.
Hint 3
Print "T" or "F" for each value.
Solution
Show one correct solution (try it yourself first)
formula = input()
for p in (True, False):
for q in (True, False):
value = eval(formula, {"p": p, "q": q})
print("T" if p else "F", "T" if q else "F", "T" if value else "F")