Reputation: 1900
The goal is to create a program which will effectively let the user create boolean logic statements (not started ), store those expressions, access them (expression_menu), build them (setup_table) and then evaluate them (truth) then finally test them (in another module).
Given all that, which is a fairly large project for my skills. I'm stuck on how to organize everything. I feel like i might want to be moving towards using classes because it might be easier to keep track of attributes...
Immediately however my problem is that how to transfer around the boolean logic statements, obviously on line 29 i'll get a syntax error because x is undefined (the snippet of code only makes sense in lines 11 through 15.
How can i organize my code here to better suit my goal
def setup_table(variables=2):
return (list(itertools.product(range(2), repeat = variables)))
def truth(variables=None,expression=None):
truth_table = []
for x in setup_table(variables):
if expression:
x.append(1)
else:
x.append(0)
truth_table.append(x)
return truth_table
def expression_menu():
expression = input('''
choose your expression:
1. if ((p and q) or (p or q)) and not(r or not q):
2. if (p or r) or (q and s):
3. if (p or r) and ( q or (p and s))
Expression: ''')
table = None
if int(expression) == 1:
table = truth(variables = 3, expression =((x[0] and x[1]) or (x[0] or x[1])) and not (x[
print(table)
if __name__ == "__main__":
import itertools
expression_menu()
Upvotes: 1
Views: 236
Reputation: 82026
You could make your boolean expression into a function.
So:
table = truth(variables = 3, expression = lambda x: (x[0] and x[1]))
or:
def expression(x):
return x[0] and x[1]
table = truth(variables = 3, expression = expression)
Upvotes: 1