Creating a truth table for any expression in Python

前端 未结 3 719
青春惊慌失措
青春惊慌失措 2021-02-09 17:42

I am attempting to create a program that when run will ask for the boolean expression, the variables and then create a truth table for whatever is entered. I need to use a class

3条回答
  •  醉梦人生
    2021-02-09 18:12

    You probably want to do something like this:

    from itertools import product
    for p in product((True, False), repeat=len(variables)):
        # Map variable in variables to value in p
        # Apply boolean operators to variables that now have values
        # add result of each application to column in truth table
        pass
    

    But the inside of the for loop is the hardest part, so good luck.

    This is an example of what you would be iterating over in the case of three variables:

    >>> list(product((True, False), repeat=3))
    [(True, True, True), (True, True, False), (True, False, True), (True, False, False), (False, True, True), (False, True, False), (False, False, True), (False, False, False)]
    

提交回复
热议问题