0

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()
3
  • is your truth_table really inside menu()? Commented Sep 19, 2019 at 2:49
  • I'm pretty sure Commented Sep 19, 2019 at 2:56
  • @SSmith, I edited your code in my answer. Give it a try Commented Sep 19, 2019 at 2:59

3 Answers 3

1

This is just another indention issues. You are calling truth_table() in menu() just before it was assigned. You can outdent the def truth_table(): line until your except line by one tab or 4 spaces.

You code should look like this and you're good to go:

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Just dedenting doesn't help. Now you've cut the menu function in half...
@ShadowRanger, outdenting doesnt cut the menu half, as you can see truth_table is really a different function out from the menu
0

Try this. The error you mentioned doesn't appear any more. (There could be other design error, but this solves the problem you mentioned, at least).

import itertools
import sys

def menu() :

    def truth_table():
        inps = None
        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")
        return inps

    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":
        inps = truth_table()
    elif choice =="3":
        print("Program Terminated")
        sys.exit()
    else:
        print ("Invalid input")
        menu()

    if inps is not None:
        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() 

1 Comment

@S.Smith If this solution worked for you, please consider voting-up and accepting the answer. Thank you.
0

You need to move the definition of the truth_table function either:

  1. About the if/else blocks that use it, or
  2. Out of menu entirely (dedent it, and put either before or after the menu function)

As is, it's a local variable to menu, which is only assigned when you reach the def truth_table line, but you're trying to call it before then.

2 Comments

How do I put it out of the main entirely?
@S.Smith: Sorry, main was a mistake. Move it out of menu, in exactly the manner I described (dedent it, and cut/paste it somewhere not in the indented body of menu, e.g. just above the def menu line).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.