truth table: test truth table tests


I want to write a program that makes the tests in tests/test_binary.py and tests/test_nullary_unary.py pass without looking at them

requirements

open the project

  • I make sure I am in the pumping_python folder with pwd in the terminal

    pwd
    

    if the terminal does not show

    .../pumping_python
    

    I change directory to the pumping_python folder

  • I change directory to the truth_table project

    cd truth_table
    

    the terminal shows

    .../pumping_python/truth_table
    
  • I run the tests with pytest-watcher

    uv run pytest-watcher . --now
    

    the terminal is my friend, and shows

    rootdir: .../pumping_python/truth_table
    configfile: pyproject.toml
    collected 20 items
    
    tests/test_binary.py ....................          [ 80%]
    tests/test_nullary_unary.py ....                   [100%]
    
    ================== 20 passed in G.HIs ===================
    

RED: make it fail

  • I open __init__.py from the truth_table folder in the src folder

  • I delete everything in src/truth_table/__init__.py, the terminal is my friend, and shows 20 failures, I start with the last one

    AttributeError: module 'src.truth_table'
                    has no attribute 'logical_negation'
    

    because …

Can you make the tests pass without looking at how I solve it below? You can come back to compare solutions when you are done or if you get stuck.


GREEN: make it pass

  • I add the name to src/truth_table/__init__.py

    1logical_negation
    

    the terminal is my friend, and shows NameError

    NameError: name 'logical_negation' is not defined
    
  • I point it to None to define it

    1logical_negation = None
    

    the terminal is my friend, and shows TypeError

    TypeError: 'NoneType' object is not callable
    

    because I cannot call None like a function.

  • I make logical_negation a function

    1# logical_negation = None
    2def logical_negation():
    3    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: logical_negation() takes
               0 positional arguments but 1 was given
    
  • I add a name in parentheses

    1# logical_negation = None
    2# def logical_negation():
    3def logical_negation(something):
    4    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None is not true
    

    the assertion expects True.

  • I change None in the return statement to give the test what it wants

    1# logical_negation = None
    2# def logical_negation():
    3def logical_negation(something):
    4    return True
    5    return None
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    the assertion expects False and the function returns True.

  • I change the return statement

    1# logical_negation = None
    2# def logical_negation():
    3def logical_negation(something):
    4    return False
    5    return True
    6    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    
  • I change the return statement to see the difference between the input and what the assertion expects, remember the identity_function?

    1# logical_negation = None
    2# def logical_negation():
    3def logical_negation(something):
    4    return something
    5    return False
    6    return True
    7    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    
    • from what I have seen so far, this test expects

  • I use not in the return statement

    1# logical_negation = None
    2# def logical_negation():
    3def logical_negation(something):
    4    return not something
    5    return something
    6    return False
    7    return True
    8    return None
    

    the terminal shows 19 failed, 1 passed, progress! It also shows AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'logical_identity'
    
  • I add a function definition for logical_identity

     8    return None
     9
    10
    11def logical_identity():
    12    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: logical_identity() takes
               0 positional arguments but 1 was given
    
  • I add a name in parentheses

    11# def logical_identity():
    12def logical_identity(something):
    13    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None is not true
    
  • I return what the test wants

    11# def logical_identity():
    12def logical_identity(something):
    13    return True
    14    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    the assertion expects False and the function returns True.

  • I change the return statement

    11# def logical_identity():
    12def logical_identity(something):
    13    return False
    14    return True
    15    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    from what I have seen so far, this test expects

    • True when the function returns False.

    • False when the function returns True.

    • True when the function returns None (which is grouped as False).

  • I change the return statement to see the difference between the input and what the assertion expects

    11# def logical_identity():
    12def logical_identity(something):
    13    return something
    14    return False
    15    return True
    16    return None
    

    the terminal shows 18 failed, 2 passed, more progress! It also shows AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'logical_true'
    
  • I add logical_true

    16    return None
    17
    18
    19def logical_true():
    20    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None is not true
    
  • I change the return statement for logical_true to give the test what it wants

    19def logical_true():
    20    return True
    21    return None
    

    17 failed, 3 passed. That was simple. the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'logical_false'.
                    Did you mean: 'logical_true'?
    
  • I add logical_false

    21    return None
    22
    23
    24def logical_false():
    25    return None
    

    the terminal shows 16 failed, 4 passed, simple again. I am getting it. The terminal also shows AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'tautology'
    
  • I add tautology

    25    return None
    26
    27
    28def tautology():
    29    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: tautology() takes
               0 positional arguments but 2 were given
    
  • I add two names in parentheses for the function to take input arguments

    28# def tautology():
    29def tautology(first, second):
    30    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None is not true
    
  • I change None to True in the return statement

    28# def tautology():
    29def tautology(first, second):
    30    return True
    31    return None
    

    the terminal shows 15 failed, 5 passed, Yes! It also shows AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'project_second'
    
  • I add project_second

    31    return None
    32
    33
    34def project_second():
    35    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: project_second() takes
               0 positional arguments but 2 were given
    
  • I add two names in parentheses for the function to take input arguments

    34# def project_second():
    35def project_second(first, second):
    36    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None is not true
    
  • I change the return statement to give the test what it wants

    34# def project_second():
    35def project_second(first, second):
    36    return True
    37    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    the assertion expects False and the function returns True.

  • I change the return statement

    34# def project_second():
    35def project_second(first, second):
    36    return False
    37    return True
    38    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    from what I have seen so far, this test expects

    • True when the function returns False.

    • False when the function returns True.

    • True when the function returns None (which is grouped as False).

  • I change the return statement to see the difference between the input and what the assertion expects

    34# def project_second():
    35def project_second(first, second):
    36    return first, second
    37    return False
    38    return True
    39    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, False) is not false
    

    second is False and is equal to the expectation of the assertion.

  • I remove first from the return statement since second is False

    34# def project_second():
    35def project_second(first, second):
    36    return second
    37    return first, second
    38    return False
    39    return True
    40    return None
    

    the terminal shows 14 failed, 6 passed, and AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'project_first'
    
  • I add a function definition for project_first

    40    return None
    41
    42
    43def project_first():
    44    return None
    

    the terminal is my friend, and shows TypeError

    TypeError: project_first() takes
               0 positional arguments but 2 were given
    

    okay, I have seen this before.

  • I add two names in the parentheses

    43# def project_first():
    44def project_first(first, second):
    45    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None is not true
    
  • I change the return statement to give the test what it wants

    43# def project_first():
    44def project_first(first, second):
    45    return True
    46    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    the assertion expects False and the function returns True.

  • I change the return statement

    43# def project_first():
    44def project_first(first, second):
    45    return False
    46    return True
    47    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    this test expects

    • True when the function returns False.

    • False when the function returns True.

    • True when the function returns None (which is grouped as False).

  • I change the return statement to see the difference between the input and what the assertion expects

    43# def project_first():
    44def project_first(first, second):
    45    return first, second
    46    return False
    47    return True
    48    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (False, True) is not false
    

    first is False and is equal to the expectation of the assertion.

  • I remove second from the return statement since first is False

    43# def project_first():
    44def project_first(first, second):
    45    return first
    46    return first, second
    47    return False
    48    return True
    49    return None
    

    the terminal shows 13 failed, 7 passed, and AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'negate_second'
    
  • I add a function definition for negate_second with a return statement to see the difference between the input and what the assertion expects

    49    return None
    50
    51
    52def negate_second(first, second):
    53    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, True) is not false
    
  • I add an if statement for (True, True) to negate_second since first and second are both not False

    52def negate_second(first, second):
    53    if (first, second) == (True, True): return False
    54    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (False, True) is not false
    
  • I add an if statement for (False, True) to negate_second

    52def negate_second(first, second):
    53    if (first, second) == (True, True): return False
    54    if (first, second) == (False, True): return False
    55    return first, second
    

    the terminal is my friend, and shows 12 failed, 8 passed with AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'negate_first'
    
  • I add a function definition for negate_first with a return statement to see the difference between the input and what the assertion expects

    55    return first, second
    56
    57
    58def negate_first(first, second):
    59    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, True)
    
  • I add an if statement for (True, True) to negate_first since first and second are both not False

    58def negate_first(first, second):
    59    if (first, second) == (True, True): return False
    60    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, False) is not false
    
  • I add an if statement for (True, False) to negate_first

    58def negate_first(first, second):
    59    if (first, second) == (True, True): return False
    60    if (first, second) == (True, False): return False
    61    return first, second
    

    the terminal is my friend, and shows 11 failed, 9 passed with AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'material_non_implication'
    
  • I add a function definition for material_non_implication with a return statement to see the difference between the input and what the assertion expects

    61    return first, second
    62
    63
    64def material_non_implication(first, second):
    65    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, True) is not false
    
  • I add an if statement for (True, True) to material_non_implication since first and second are both not False

    64def material_non_implication(first, second):
    65    if (first, second) == (True, True): return False
    66    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (False, True) is not false
    
  • I add an if statement for (False, True) to material_non_implication

    64def material_non_implication(first, second):
    65    if (first, second) == (True, True): return False
    66    if (first, second) == (False, True): return False
    67    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (False, False) is not false
    
  • I add an if statement for (False, False) to material_non_implication

    64def material_non_implication(first, second):
    65    if (first, second) == (True, True): return False
    66    if (first, second) == (False, True): return False
    67    if (first, second) == (False, False): return False
    68    return first, second
    

    the terminal is my friend, and shows 10 failed, 10 passed with AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'material_implication'
    
  • I add a function definition for material_implication with a return statement to see the difference between the input and what the assertion expects

    68    return first, second
    69
    70
    71def material_implication(first, second):
    72    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, False) is not false
    
  • I add an if statement for (True, False) to material_implication

    71def material_implication(first, second):
    72    if (first, second) == (True, False): return False
    73    return first, second
    

    the terminal is my friend, and shows 9 failed, 11 passed with AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'logical_nor'
    
  • I add a function definition for logical_nor

    73    return first, second
    74
    75
    76def logical_nor(first, second):
    77    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, True) is not false
    
  • I add an if statement for (True, True) to logical_nor

    76def logical_nor(first, second):
    77    if (first, second) == (True, True): return False
    78    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, False) is not false
    
  • I add an if statement for (True, False) to logical_nor

    76def logical_nor(first, second):
    77    if (first, second) == (True, True): return False
    78    if (first, second) == (True, False): return False
    79    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (False, True) is not false
    
  • I add an if statement for (False, True) to logical_nor

    76def logical_nor(first, second):
    77    if (first, second) == (True, True): return False
    78    if (first, second) == (True, False): return False
    79    if (first, second) == (False, True): return False
    80    return first, second
    

    the terminal is my friend, and shows 8 failed, 12 passed with AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'logical_nand'.
                    Did you mean: 'logical_nor'?
    
  • I add a function definition for logical_nand

    80    return first, second
    81
    82
    83def logical_nand(first, second):
    84    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, True) is not false
    
  • I add an if statement for (True, True) to logical_nand

    83def logical_nand(first, second):
    84    if (first, second) == (True, True): return False
    85    return first, second
    

    the terminal is my friend, and shows 7 failed, 13 passed with AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'logical_equality'.
                    Did you mean: 'logical_identity'?
    
  • I add logical_equality

    85    return first, second
    86
    87
    88def logical_equality(first, second):
    89    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, False) is not false
    
  • I add an if statement for (True, False) to logical_equality

    88def logical_equality(first, second):
    89    if (first, second) == (True, False): return False
    90    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (False, True) is not false
    
  • I add an if statement for (False, True) to logical_equality

    88def logical_equality(first, second):
    89    if (first, second) == (True, False): return False
    90    if (first, second) == (False, True): return False
    91    return first, second
    

    the terminal is my friend, and shows 6 failed, 14 passed with AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'logical_disjunction'
    
  • I add a function definition for logical_disjunction

    91    return first, second
    92
    93
    94def logical_disjunction(first, second):
    95    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (False, False) is not false
    
  • I add an if statement for (False, False) to logical_disjunction

    94def logical_disjunction(first, second):
    95    if (first, second) == (False, False): return False
    96    return first, second
    

    the terminal is my friend, and shows 5 failed, 15 passed with AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'logical_conjunction'.
                    Did you mean: 'logical_disjunction'?
    
  • I add a function definition for logical_conjunction

     96    return first, second
     97
     98
     99def logical_conjunction(first, second):
    100    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, False) is not false
    
  • I add an if statement for (True, False) to logical_conjunction

     99def logical_conjunction(first, second):
    100    if (first, second) == (True, False): return False
    101    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (False, True) is not false
    
  • I add an if statement for (False, True) to logical_conjunction

     99def logical_conjunction(first, second):
    100    if (first, second) == (True, False): return False
    101    if (first, second) == (False, True): return False
    102    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (False, False) is not false
    
  • I add an if statement for (False, False) to logical_conjunction

     99def logical_conjunction(first, second):
    100    if (first, second) == (True, False): return False
    101    if (first, second) == (False, True): return False
    102    if (first, second) == (False, False): return False
    103    return first, second
    

    the terminal is my friend, and shows 4 failed, 16 passed with AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'exclusive_disjunction'
    
  • I add a function definition for exclusive_disjunction

    103    return first, second
    104
    105
    106def exclusive_disjunction(first, second):
    107    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, True) is not false
    
  • I add an if statement for (True, True) to exclusive_disjunction

    106def exclusive_disjunction(first, second):
    107    if (first, second) == (True, True): return False
    108    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (False, False) is not false
    
  • I add an if statement for (False, False) to exclusive_disjunction

    106def exclusive_disjunction(first, second):
    107    if (first, second) == (True, True): return False
    108    if (first, second) == (False, False): return False
    109    return first, second
    

    the terminal is my friend, and shows 3 failed, 17 passed with AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'converse_non_implication'.
                    Did you mean: 'material_non_implication'?
    
  • I add converse_non_implication

    109    return first, second
    110
    111
    112def converse_non_implication(first, second):
    113    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, True) is not false
    
  • I add an if statement for (True, True) to converse_non_implication

    112def converse_non_implication(first, second):
    113    if (first, second) == (True, True): return False
    114    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, False) is not false
    
  • I add an if statement for (True, False) to converse_non_implication

    112def converse_non_implication(first, second):
    113    if (first, second) == (True, True): return False
    114    if (first, second) == (True, False): return False
    115    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (False, False) is not false
    
  • I add an if statement for (False, False) to converse_non_implication

    112def converse_non_implication(first, second):
    113    if (first, second) == (True, True): return False
    114    if (first, second) == (True, False): return False
    115    if (first, second) == (False, False): return False
    116    return first, second
    

    the terminal is my friend, and shows 2 passed, 18 failed for AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'converse_implication'.
                    Did you mean: 'converse_non_implication'?
    
  • I add converse_implication

    116    return first, second
    117
    118
    119def converse_implication(first, second):
    120    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (False, True) is not false
    
  • I add an if statement for (False, True) to converse_implication

    119def converse_implication(first, second):
    120    if (first, second) == (False, True): return False
    121    return first, second
    

    the terminal is my friend, and shows 1 failed, 19 passed with AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'contradiction'
    
  • I add contradiction

    121    return first, second
    122
    123
    124def contradiction(first, second):
    125    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, True) is not false
    
  • I add an if statement for (True, True) to contradiction

    124def contradiction(first, second):
    125    if (first, second) == (True, True): return False
    126    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (True, False) is not false
    
  • I add an if statement for (True, False) to contradiction

    124def contradiction(first, second):
    125    if (first, second) == (True, True): return False
    126    if (first, second) == (True, False): return False
    127    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (False, True) is not false
    
  • I add an if statement for (False, True) to contradiction

    124def contradiction(first, second):
    125    if (first, second) == (True, True): return False
    126    if (first, second) == (True, False): return False
    127    if (first, second) == (False, True): return False
    128    return first, second
    

    the terminal is my friend, and shows AssertionError

    AssertionError: (False, False) is not false
    
  • I add an if statement for (False, False) to contradiction

    124def contradiction(first, second):
    125    if (first, second) == (True, True): return False
    126    if (first, second) == (True, False): return False
    127    if (first, second) == (False, True): return False
    128    if (first, second) == (False, False): return False
    129    return first, second
    

    the terminal is my friend, and shows

    ================== 20 passed in G.HIs ===================
    

All the tests are passing! And the world is a better place than when I started.


REFACTOR: make it better

Wait, there is more… Since all the tests are passing, I can play with the functions I have to make them simpler and understand why my solutions work.

  • contradiction returns False in four cases. There are only four cases with two inputs. I add a return statement

    124def contradiction(first, second):
    125    return False
    126    if (first, second) == (True, True): return False
    127    if (first, second) == (True, False): return False
    128    if (first, second) == (False, True): return False
    129    if (first, second) == (False, False): return False
    130    return first, second
    

    the test is still green.

  • I remove the commented lines from contradiction

    124def contradiction(first, second):
    125    return False
    

  • converse_implication returns False in only one case. I write out the if statement for that case, to make it clearer

    119def converse_implication(first, second):
    120    # if (first, second) == (False, True): return False
    121    if first == False and second == True:
    122        return False
    123    return first, second
    

    still green.

  • I change the statement with not and True

    119def converse_implication(first, second):
    120    # if (first, second) == (False, True): return False
    121    # if first == False and second == True:
    122    if not first == True and second == True:
    123        return False
    124    return first, second
    

    green.

  • I remove == True

    119def converse_implication(first, second):
    120    # if (first, second) == (False, True): return False
    121    # if first == False and second == True:
    122    # if not first == True and second == True:
    123    if not first and second:
    124        return False
    125    return first, second
    

    still green.

  • I add a return statement because if something: return False can be written as return not (something)

    119def converse_implication(first, second):
    120    return not (not first and second)
    121    # if (first, second) == (False, True): return False
    122    # if first == False and second == True:
    123    if not first and second:
    124        return False
    125    return first, second
    

    the test is still green.

  • I “multiply not” by the things in the parentheses

    119def converse_implication(first, second):
    120    return (not not first) (not and) (not second)
    121    return not (not first and second)
    

    the terminal is my friend, and shows SyntaxError

    SyntaxError: invalid syntax
    
  • I change not and to or

    119def converse_implication(first, second):
    120    # return (not not first) (not and) (not second)
    121    return (not not first) or (not second)
    122    return not (not first and second)
    

    the test is green again.

  • I remove not not because two nots make a right?

    119def converse_implication(first, second):
    120    return first or not second
    121    # return (not not first) (not and) (not second)
    122    return (not not first) or (not second)
    

    the test is still green.

  • I remove the commented lines and other statements from converse_implication

    119def converse_implication(first, second):
    120    return first or not second
    121
    122
    123def contradiction(first, second):
    124    return False
    

  • I add an if statement for the only case that returns True in converse_non_implication

    112def converse_non_implication(first, second):
    113    if first == False and second == True:
    114        return True
    115    else:
    116        return False
    117    # if (first, second) == (True, True): return False
    118    # if (first, second) == (True, False): return False
    119    # if (first, second) == (False, False): return False
    120    # return first, second
    121
    122
    123def converse_implication(first, second):
    

    the test is still green.

  • I change if first == False to terms of True

    112def converse_non_implication(first, second):
    113    # if first == False and second == True:
    114    if not first == True and second == True:
    115        return True
    116    else:
    117        return False
    

    green.

  • I remove == True

    112def converse_non_implication(first, second):
    113    # if first == False and second == True:
    114    # if not first == True and second == True:
    115    if not first and second:
    116        return True
    117    else:
    118        return False
    

    still green.

  • I add a return statement because if something: return True can be written as return something since something is grouped as True

    112def converse_non_implication(first, second):
    113    return not first and second
    114    # if first == False and second == True:
    

    the test is still green.

  • I remove the commented lines and other statements from converse_non_implication

    112def converse_non_implication(first, second):
    113    return not first and second
    114
    115
    116def converse_implication(first, second):
    117    return first or not second
    

  • I make the if statements in exclusive_disjunction simpler

    106def exclusive_disjunction(first, second):
    107    if first == True and second == True:
    108        return False
    109    if first == False and second == False:
    110        return False
    111    return True
    112    if (first, second) == (True, True): return False
    113    if (first, second) == (False, False): return False
    114    return first, second
    

    the test is still green.

  • I change the new second if statement with not and True

    106def exclusive_disjunction(first, second):
    107    if first == True and second == True:
    108        return False
    109    # if first == False and second == False:
    110    if not first == True and not second == True:
    111        return False
    112    return True
    

    still green.

  • I remove == True

    106def exclusive_disjunction(first, second):
    107    # if first == True and second == True:
    108    if first and second:
    109        return False
    110    # if first == False and second == False:
    111    # if not first == True and not second == True:
    112    if not first and not second:
    113        return False
    114    return True
    

    green.

  • I write everything in the second if statement with not because it happens two times in the line, I might as well make it three

    106def exclusive_disjunction(first, second):
    107    # if first == True and second == True:
    108    if first and second:
    109        return False
    110    # if first == False and second == False:
    111    # if not first == True and not second == True:
    112    # if not first and not second:
    113    if (not first) (not or) (not second):
    114        return False
    115    return True
    

    the terminal is my friend, and shows SyntaxError

    SyntaxError: invalid syntax
    
  • I factor out the nots

    106def exclusive_disjunction(first, second):
    107    # if first == True and second == True:
    108    if first and second:
    109        return False
    110    # if first == False and second == False:
    111    # if not first == True and not second == True:
    112    # if not first and not second:
    113    # if (not first) (not or) (not second):
    114    if not (first or second):
    115        return False
    116    return True
    

    the test is green .

  • I put the two if statements together as one

    106def exclusive_disjunction(first, second):
    107    if (
    108        (first and second)
    109        or
    110        (not (first or second))
    111    ):
    112        return False
    113    return True
    114    # if first == True and second == True:
    

    the test is still green.

  • I add a return statement because if something: return False can be written as return not (something)

    106def exclusive_disjunction(first, second):
    107    return not (
    108        (first and second)
    109        or
    110        (not (first or second))
    111    )
    112    if (
    113        (first and second)
    114        or
    115        (not (first or second))
    116    ):
    

    still green.

  • I “multiply” not by everything in the parentheses since it happens two times in the line

    106def exclusive_disjunction(first, second):
    107    return (
    108        (not (first and second))
    109        (not or)
    110        (not (not (first or second)))
    111    )
    112    return not (
    113        (first and second)
    114        or
    115        (not (first or second))
    116    )
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    
  • I change not or to and

    106def exclusive_disjunction(first, second):
    107    return (
    108        (not (first and second))
    109        # (not or)
    110        and
    111        (not (not (first or second)))
    112    )
    

    the test is green again.

  • I remove not not because “the negation of a negation is …”

    106def exclusive_disjunction(first, second):
    107    return (
    108        (not (first and second))
    109        # (not or)
    110        and
    111        # (not (not (first or second)))
    112        (first or second)
    113    )
    

    green.

  • I remove the commented lines and other statements from exclusive_disjunction

    106def exclusive_disjunction(first, second):
    107    return (
    108        (not (first and second))
    109        and
    110        (first or second)
    111    )
    112
    113
    114def converse_non_implication(first, second):
    115    return not first and second
    

  • logical_conjunction has only one case that returns True. I add an if statement for it

     99def logical_conjunction(first, second):
    100    if first == True and second == True:
    101        return True
    102    else:
    103        return False
    104    if (first, second) == (True, False):
    

    the test is still green.

  • I remove == True

     99def logical_conjunction(first, second):
    100    # if first == True and second == True:
    101    if first and second:
    102        return True
    103    else:
    104        return False
    

    still green.

  • I add a return statement because if something: return True can be written as return something since something is grouped as True

     99def logical_conjunction(first, second):
    100    return first and second
    101    # if first == True and second == True:
    

    green.

  • I remove the commented line and other statements from logical_conjunction

     99def logical_conjunction(first, second):
    100    return first and second
    101
    102
    103def exclusive_disjunction(first, second):
    

  • logical_disjunction has only one case that returns False, I break up the if statement

    94def logical_disjunction(first, second):
    95    if first == False and second == False:
    96        return False
    97    else:
    98        return True
    99    if (first, second) == (False, False):
    

    the test is still green.

  • I change the if statement with not and True

    94def logical_disjunction(first, second):
    95    # if first == False and second == False:
    96    if not first == True and not second == True:
    97        return False
    98    else:
    99        return True
    

    still green.

  • I remove == True

     94def logical_disjunction(first, second):
     95    # if first == False and second == False:
     96    # if not first == True and not second == True:
     97    if not first and not second:
     98        return False
     99    else:
    100        return True
    

    green.

  • I change the statement with not since it happens two times

     94def logical_disjunction(first, second):
     95    # if first == False and second == False:
     96    # if not first == True and not second == True:
     97    # if not first and not second:
     98    if (not first) (not or) (not second):
     99        return False
    100    else:
    101        return True
    

    the terminal is my friend, and shows SyntaxError

    SyntaxError: invalid syntax
    
  • I factor out not

     94def logical_disjunction(first, second):
     95    # if first == False and second == False:
     96    # if not first == True and not second == True:
     97    # if not first and not second:
     98    # if (not first) (not or) (not second):
     99    if not (first or second):
    100        return False
    101    else:
    102        return True
    

    the test is green again.

  • I add a return statement for the opposite of the if statement because if something: return False can be written as return not (something)

    94def logical_disjunction(first, second):
    95    return not (not (first or second))
    96    # if first == False and second == False:
    

    still green.

  • I remove not not

    94def logical_disjunction(first, second):
    95    return first or second
    96    return not (not (first or second))
    

    the test is still green.

  • I remove the commented lines and other statements from logical_disjunction

    94def logical_disjunction(first, second):
    95    return first or second
    96
    97
    98def logical_conjunction(first, second):
    

  • I break up the if statements in logical_equality

    88def logical_equality(first, second):
    89    if first == True and second == False:
    90        return False
    91    if first == False and second == True:
    92        return False
    93    return True
    94    if (first, second) == (True, False):
    

    the test is still green.

  • I change the if statements with not and True

    88def logical_equality(first, second):
    89    # if first == True and second == False:
    90    if first == True and not second == True:
    91        return False
    92    # if first == False and second == True:
    93    if not first == True and second == True:
    94        return False
    95    return True
    

    still green.

  • I remove == True

    88def logical_equality(first, second):
    89    # if first == True and second == False:
    90    # if first == True and not second == True:
    91    if first and not second:
    92        return False
    93    # if first == False and second == True:
    94    # if not first == True and second == True:
    95    if not first and second:
    96        return False
    97    return True
    

    green.

  • I put the two if statements together as one

    88def logical_equality(first, second):
    89    if (
    90        (first and not second)
    91        or
    92        (not first and second)
    93    ):
    94        return False
    95    return True
    96    # if first == True and second == False:
    

    still green.

  • I add a return statement because …

    88def logical_equality(first, second):
    89    return not (
    90        (first and not second)
    91        or
    92        (not first and second)
    93    )
    94    if (
    95        (first and not second)
    96        or
    97        (not first and second)
    98    ):
    

    the test is still green.

  • I multiply not by all the symbols in parentheses

    88def logical_equality(first, second):
    89    return (
    90        (not (first and not second))
    91        (not or)
    92        (not (not first and second))
    93    )
    94    return not (
    95        (first and not second)
    96        or
    97        (not first and second)
    98    )
    

    the terminal is my friend, and shows SyntaxError

    SyntaxError: invalid syntax
    
  • I change not or to and

    88def logical_equality(first, second):
    89    return (
    90        (not (first and not second))
    91        # (not or)
    92        and
    93        (not (not first and second))
    94    )
    

    the test is green again.

  • I multiply not by the symbols in the first part of the statement

    88def logical_equality(first, second):
    89    return (
    90        # (not (first and not second))
    91        ((not first) (not and) (not not second))
    92        # (not or)
    93        and
    94        (not (not first and second))
    95    )
    

    the terminal is my friend, and shows SyntaxError

    SyntaxError: invalid syntax
    
  • I change not and to or

    88def logical_equality(first, second):
    89    return (
    90        # (not (first and not second))
    91        # ((not first) (not and) (not not second))
    92        ((not first) or (not not second))
    93        # (not or)
    94        and
    95        (not (not first and second))
    96    )
    

    the test is green again.

  • I remove not not

    88def logical_equality(first, second):
    89    return (
    90        # (not (first and not second))
    91        # ((not first) (not and) (not not second))
    92        # ((not first) or (not not second))
    93        (not first or second)
    94        # (not or)
    95        and
    96        (not (not first and second))
    97    )
    

    still green.

  • I multiply not by the symbols in the second part of the statement

    88def logical_equality(first, second):
    89    return (
    90        # (not (first and not second))
    91        # ((not first) (not and) (not not second))
    92        # ((not first) or (not not second))
    93        (not first or second)
    94        # (not or)
    95        and
    96        # (not (not first and second))
    97        ((not not first) (not and) (not second))
    98    )
    

    the terminal is my friend, and shows SyntaxError

    SyntaxError: invalid syntax
    
  • I change not and to or

    88def logical_equality(first, second):
    89    return (
    90        # (not (first and not second))
    91        # ((not first) (not and) (not not second))
    92        # ((not first) or (not not second))
    93        (not first or second)
    94        # (not or)
    95        and
    96        # (not (not first and second))
    97        # ((not not first) (not and) (not second))
    98        ((not not first) or (not second))
    99    )
    

    the test is green again.

  • I remove not not

     88def logical_equality(first, second):
     89    return (
     90        # (not (first and not second))
     91        # ((not first) (not and) (not not second))
     92        # ((not first) or (not not second))
     93        (not first or second)
     94        # (not or)
     95        and
     96        # (not (not first and second))
     97        # ((not not first) (not and) (not second))
     98        # ((not not first) or (not second))
     99        (first or not second)
    100    )
    

    the test is still green.

  • I remove the commented lines and other statements from logical_equality

    88def logical_equality(first, second):
    89    return (
    90        (not first or second)
    91        and
    92        (first or not second)
    93    )
    94
    95
    96def logical_disjunction(first, second):
    

  • I make the if statement in logical_nand simpler

    83def logical_nand(first, second):
    84    if first == True and second == True:
    85        return False
    86    else:
    87        return True
    88    if (first, second) == (True, True): return False
    89    return first, second
    

    the test is still green.

  • I remove == True

    83def logical_nand(first, second):
    84    # if first == True and second == True:
    85    if first and second:
    86        return False
    87    else:
    88        return True
    89    if (first, second) == (True, True):
    

    still green.

  • I add a return statement because …

    83def logical_nand(first, second):
    84    return not (first and second)
    85    # if first == True and second == True:
    

    green.

  • I remove the commented line and other statements from logical_nand

    83def logical_nand(first, second):
    84    return not (first and second)
    85
    86
    87def logical_equality(first, second):
    

  • logical_nor has only one case that returns True. I add an if statement for it

    76def logical_nor(first, second):
    77    if first == False and second == False:
    78        return True
    79    else:
    80        return False
    81    if (first, second) == (True, True):
    

    the test is still green.

  • I use not to write the if statement in terms of True

    76def logical_nor(first, second):
    77    # if first == False and second == False:
    78    if not first == True and not second == True:
    79        return True
    80    else:
    81        return False
    

    still green.

  • I remove == True

    76def logical_nor(first, second):
    77    # if first == False and second == False:
    78    # if not first == True and not second == True:
    79    if not first and not second:
    80        return True
    81    else:
    82        return False
    

    green.

  • I add a return statement

    76def logical_nor(first, second):
    77    return not first and not second
    78    # if first == False and second == False:
    

    still green.

  • I write the statement with not because it happens two times

    76def logical_nor(first, second):
    77    return (not first) (not or) (not second)
    78    return not first and not second
    

    the terminal is my friend, and shows SyntaxError

    SyntaxError: invalid syntax
    
  • I factor out not

    76def logical_nor(first, second):
    77    return not (first or second)
    78    return (not first) (not or) (not second)
    

    the test is green again.

  • I remove the commented lines and other statements from logical_nor

    76def logical_nor(first, second):
    77    return not (first or second)
    78
    79
    80def logical_nand(first, second):
    

  • I add a return statement for the if statement of material_implication because if something: return False can be written as return not (something)

    71def material_implication(first, second):
    72    return not ((first, second) == (True, False))
    73    if (first, second) == (True, False): return False
    74    return first, second
    

    the test is still green.

  • I break up the return statement to make it simpler

    71def material_implication(first, second):
    72    return not (first == True and second == False)
    73    return not ((first, second) == (True, False))
    

    still green.

  • I use not to rewrite it in terms of True

    71def material_implication(first, second):
    72    return not (first == True and not second == True)
    73    return not (first == True and second == False)
    

    green.

  • I remove == True

    71def material_implication(first, second):
    72    return not (first and not second)
    73    return not (first == True and not second == True)
    

    still green.

  • I multiply not by everything in parentheses

    71def material_implication(first, second):
    72    return (not first) (not and) (not not second)
    73    return not (first and not second)
    

    the terminal is my friend, and shows SyntaxError

    SyntaxError: invalid syntax
    
  • I change not and to or

    71def material_implication(first, second):
    72    return (not first) or (not not second)
    73    # return (not first) (not and) (not not second)
    74    return not (first and not second)
    

    the test is green again.

  • I remove not not

    71def material_implication(first, second):
    72    return not first or second
    73    return (not first) or (not not second)
    

    the test is still green.

  • I remove the commented lines from material_implication

    71def material_implication(first, second):
    72    return not first or second
    73
    74
    75def logical_nor(first, second):
    

  • material_non_implication has 3 cases that return False. I add a return statement for the case that returns True

    64def material_non_implication(first, second):
    65    return (first, second) == (True, False)
    66    if (first, second) == (True, True): return False
    

    the test is still green.

  • I make the return statement simpler

    64def material_non_implication(first, second):
    65    return first == True and second == False
    66    return (first, second) == (True, False)
    

    still green.

  • I use not to write it in terms of True

    64def material_non_implication(first, second):
    65    return first == True and not second == True
    66    return first == True and second == False
    

    green.

  • I remove == True

    64def material_non_implication(first, second):
    65    return first and not second
    66    return first == True and not second == True
    

    still green.

  • I remove the other statements from material_non_implication

    64def material_non_implication(first, second):
    65    return first and not second
    66
    67
    68def material_implication(first, second):
    

  • first is True in the two cases where negate_first returns False, I add an if statement for them

    58def negate_first(first, second):
    59    if first == True:
    60        return False
    61    else:
    62        return True
    63    # if (first, second) == (True, True): return False
    64    # if (first, second) == (True, False): return False
    65    # return first, second
    

    the test is still green.

  • I add a return statement because …

    58def negate_first(first, second):
    59    return not first == True
    60    if first == True:
    

    still green.

  • I remove == True

    58def negate_first(first, second):
    59    return not first
    60    return not first == True
    

    green.

  • I remove the commented lines and other statements from negate_first

    58def negate_first(first, second):
    59    return not first
    60
    61
    62def material_non_implication(first, second):
    

  • second is True in the two cases where negate_second returns False. I add an if statement for them

    52def negate_second(first, second):
    53    if second == True:
    54        return False
    55    else:
    56        return True
    57    # if (first, second) == (True, True): return False
    58    # if (first, second) == (False, True): return False
    59    # return first, second
    

    the test is still green.

  • I add a return statement because if something == False: can be written as return not something

    52def negate_second(first, second):
    53    return not second == True
    54    if second == True:
    

    still green.

  • I remove == True

    52def negate_second(first, second):
    53    return not second
    54    return not second == True
    

    green.

  • I remove the commented lines and other statements from negate_second

    52def negate_second(first, second):
    53    return not second
    54
    55
    56def negate_first(first, second):
    
  • I remove the commented line and other statements from project_first

    34# def project_second():
    35def project_second(first, second):
    36    return second
    37    return first, second
    38    return False
    39    return True
    40    return None
    41
    42
    43def project_first(first, second):
    44    return first
    45
    46
    47def negate_second(first, second):
    
  • I remove the commented line and other statements from project_second

    28# def tautology():
    29def tautology(first, second):
    30    return True
    31    return None
    32
    33
    34def project_second(first, second):
    35    return second
    36
    37
    38def project_first(first, second):
    
  • I remove the commented line and second return statement from tautology

    24def logical_false():
    25    return None
    26
    27
    28def tautology(first, second):
    29    return True
    30
    31
    32def project_second(first, second):
    
  • I change the return statement of logical_false since None is grouped as False

    24def logical_false():
    25    return False
    26    return None
    

    the test is still green.

  • I remove the second return statement from logical_false and logical_true

    11# def logical_identity():
    12def logical_identity(something):
    13    return something
    14    return False
    15    return True
    16    return None
    17
    18
    19def logical_true():
    20    return True
    21
    22
    23def logical_false():
    24    return False
    25
    26
    27def tautology(first, second):
    
  • I remove the commented lines and other statements from logical_identity and logical_negation

    1def logical_negation(something):
    2    return not something
    3
    4
    5def logical_identity(something):
    6    return something
    7
    8
    9def logical_true():
    
  • I add a git commit message in the other terminal

    git commit -am 'test truth_table tests'
    

close the project

  • I close src/truth_table/__init__.py

  • I click in the terminal where the tests are running

  • I use q on the keyboard to leave the tests. The terminal goes back to the command line.

  • I change directory to the parent of truth_table

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory.


review

I ran tests using booleans which can be False or True, for the operations of the Truth Table from Mathematics

and

All the binary operations or conditions have been written with some or all of the above 3.


code from the chapter

Do you want to see all the CODE I typed for the Truth Table?


what is next?

Would you like to test projects that use the Truth Table?


rate pumping python

If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.