Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need a python code that will take all Boolean expressions and create a truth t

ID: 648188 • Letter: I

Question

I need a python code that will take all Boolean expressions and create a truth table.

If the user inputs (x or y) I need get a truth table for that. If the user inputs (x and y) or z I need a truth table for that.

This what I have so far.

n1=raw_input("Give an expression :")

n2=raw_input("Give names of variables :")

print "The Variables in", n1 ,"are", n2, "."

print "The truth table of", n1 ,"is:"

When It is finished it should look like this.

Give an expression : (x or y) and not z

Give names of variables : x y z

The variables in

Explanation / Answer

from sympy import *
from sympy.abc import p, q, r

def get_vars():
vars = []

numVars = int(raw_input())
  
for i in xrange(numVars):
vars.append(raw_input())
return vars

def get_expr():

return str(raw_input())

def convert_to_expr(inputStr):
return eval(inputStr)

def main():
vars = get_vars()
expr = get_expr()

print "Truth table for " + str(len(vars)) + "variable(s)"
for i in enumerate(truth_table(vars, expr)):
print i

def fixtable(numvars):
"""

So if numvars=2
Returns (not necessarily in this order):
True, True
True, False
False, False
False, True
"""
if numvars is 1:
yield [True]
yield [False]
else:
for i in fixtable(numvars-1):
yield i + [True]
yield i + [False]


def truth_table(vars, expr):
"""
Takes an array of variables, vars, and displays a truth table
for each possible value combination of vars.
"""
for cond in fixtable(len(vars)):
values=dict(zip(vars,cond))
yield cond + [eval(expr)]   

if __name__ == "__main__":
main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote