What I'm trying to do is to test some logical statements by printing out the truth tables. Everything works fine besides 1 problem when I choose option 2 of the menu it says that:
local variable 'truth_table' referenced before assignment
How do fix this?
I'm not sure if I can put the truth table inside the 2nd choice instead of calling on it.
import itertools
import sys
def menu() :
print ("Testing logical equivalences")
print ("1. View instructions")
print ("2. Create a truth table")
print ("3. Exit")
choice = input ("Please select an option: ")
if choice =="1":
print("=================================================================================")
print ("sdasd")
print()
print("Examples will be shown below")
print("Any of the five inputs have to be lower case and only")
print("Example: A and B (NOT C) would look like ' a and b and(not c)'")
print("All inputs are converted to lower-case. Do NOT use '' marks!")
print()
print("LIMITS OF THE APP ===============================================================")
print("1. App won't allow any inputs beside (a,b,c,d,e)")
print("2. To ensure correct use of parentheses (inner statements are evaluated first)")
print("3. The limit of inputs is 5")
print("4. Evaluation of the logical statement will be print in the next availabe column")
print("5. If your statement can't be evaluate, check for syntax and brackets")
print()
print()
wait = input("Press ENTER to return to the menu. ")
menu()
elif choice =="2":
truth_table()
elif choice =="3":
print("Program Terminated")
sys.exit()
else:
print ("Invalid input")
menu()
def truth_table():
while True:
try:
inps = int(input("Please enter the number of inputs you want 1 - 5. "))
if inps <1 or inps>5:
print ("1 input minimum, 5 max")
else:
break
except:
ValueError
print ("You must input a number between 1 and 5")
truths = list(itertools.product([False,True], repeat=inps))
statement = input("Please input the logical statement e.g. (a and b) not c.")
statement = statement.lower()
print ("A AND B OR C")##changeme A is item[0], B item[1] ...E item[4] etc.
print ("A\t\tB\t\tC\t\tD\t\tE\t\tF")
print("-"*20*inps)
for item in truths:
pos = 0
if inps == 1:
a = item[0]
elif inps == 2:
a,b = item[0], item[1]
elif inps == 3:
a,b,c = item[0], item[1], item[2]
elif inps == 4:
a,b,c,d = item[0], item[1], item[2], item[3]
else:
a,b,c,d,e = item[0], item[1], item[2], item[3], item[4]
pos = 0
while pos < inps:
print (item[pos],end = "\t\t")
pos += 1
try:
truth = eval(statement) ###turns user input into code
print (truth)
except:
print ("Unable to evaluate. Check statement")
print()
wait = input("Press ENTER to return to the menu. ")
menu()
menu()
truth_tablereally insidemenu()?