truth table: test_truth_table_tests


I want to write a program that makes the tests in test_truth_table.py pass without looking at them

requirements

continue the project

  • Make sure you are in the pumping_python folder with pwd in the terminal

    pwd
    

    if the terminal shows anything other than

    .../pumping_python
    

    change directory to the pumping_python folder

  • Once in pumping_python, change directory to the 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 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 truth_table.py from the src folder

  • I delete everything in truth_table.py, the terminal shows 20 failures, I start with the last one

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

GREEN: make it pass

  • I add the name to truth_table.py

    1logical_negation
    

    the terminal shows NameError

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

    1logical_negation = None
    

    the terminal shows TypeError

    TypeError: 'NoneType' object is not callable
    

    I cannot call None the way I can call a function

  • I make it a function

    1def logical_negation():
    2    return None
    

    the terminal shows TypeError

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

    1def logical_negation(something):
    2    return None
    

    the terminal shows AssertionError

    AssertionError: None is not true
    

    the test expects True

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

    1def logical_negation(something):
    2    return True
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    the test expects False if the function returns True

  • I change the return statement

    1def logical_negation(something):
    2    return False
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    the test expects

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

    1def logical_negation(something):
    2    return something
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    the test expects

  • I use not in the return statement

    1def logical_negation(something):
    2    return not something
    

    1 test passes with 19 failures to go, progress! The terminal shows AttributeError

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

    1def logical_negation(something):
    2    return not something
    3
    4
    5def logical_identity():
    6    return None
    

    the terminal shows TypeError

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

    5def logical_identity(something):
    6    return None
    

    the terminal shows AssertionError

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

    5def logical_identity(something):
    6    return True
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    the test expects False if the function returns True

  • I change the return statement

    5def logical_identity(something):
    6    return False
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    the test expects

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

    5def logical_identity(something):
    6    return something
    

    18 failed, 2 passed, more progress! The terminal shows AttributeError

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

     5def logical_identity(something):
     6    return something
     7
     8
     9def logical_true():
    10    return None
    

    the terminal shows AssertionError

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

     9def logical_true():
    10    return True
    

    17 failed, 3 passed. That was simple. The terminal shows AttributeError

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

     9def logical_true():
    10    return True
    11
    12
    13def logical_false():
    14    return None
    

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

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

    13def logical_false():
    14    return None
    15
    16
    17def tautology():
    18    return None
    

    the terminal 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

    17def tautology(first, second):
    18    return None
    

    the terminal shows AssertionError

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

    17def tautology(first, second):
    18    return True
    

    15 failed, 5 passed, Ayy! the terminal shows AttributeError

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

    17def tautology(first, second):
    18    return True
    19
    20
    21def project_second():
    22    return None
    

    the terminal 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

    21def project_second(first, second):
    22    return None
    

    the terminal shows AssertionError

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

    21def project_second(first, second):
    22    return False
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    the test expects True if the function returns False

  • I change the return statement

    21def project_second(first, second):
    22    return True
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    the test expects

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

    21def project_second(first, second):
    22    return first, second
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    
  • I remove first from the return statement since second is False

    21def project_second(first, second):
    22    return second
    

    14 failed, 6 passed. The terminal shows AttributeError

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

    21def project_second(first, second):
    22    return second
    23
    24
    25def project_first():
    26    return None
    

    the terminal shows TypeError

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

    okay, I have seen this before

  • I add 2 names in parentheses

    41def project_first(first, second):
    42    return None
    

    the terminal shows AssertionError

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

    41def project_first(first, second):
    42    return True
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    the test expects False if the function returns True

  • I change the return statement

    41def project_first(first, second):
    42    return False
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    the test expects

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

    41def project_first(first, second):
    42    return first, second
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    
  • I remove second from the return statement since first is False

    41def project_first(first, second):
    42    return first
    

    13 failed, 7 passed. The terminal shows AttributeError

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

    41def project_first(first, second):
    42    return first
    43
    44
    45def negate_second(first, second):
    46    return first, second
    

    the terminal shows AssertionError

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

    29def negate_second(first, second):
    30    if (first, second) == (True, True): return False
    31    return first, second
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    
  • I add another if statement

    29def negate_second(first, second):
    30    if (first, second) == (True, True): return False
    31    if (first, second) == (False, True): return False
    32    return first, second
    

    the terminal shows AttributeError

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

    29def negate_second(first, second):
    30    if (first, second) == (True, True): return False
    31    if (first, second) == (False, True): return False
    32    return first, second
    33
    34
    35def negate_first(first, second):
    36    return first, second
    

    the terminal shows AssertionError

    AssertionError: (True, True)
    
  • I add an if statement

    35def negate_first(first, second):
    36    if (first, second) == (True, True): return False
    37    return first, second
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    
  • I add another if statement

    35def negate_first(first, second):
    36    if (first, second) == (True, True): return False
    37    if (first, second) == (True, False): return False
    38    return first, second
    

    the terminal shows AttributeError

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

    35def negate_first(first, second):
    36    if (first, second) == (True, True): return False
    37    if (first, second) == (True, False): return False
    38    return first, second
    39
    40
    41def material_non_implication(first, second):
    42    return first, second
    

    the terminal shows AssertionError

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

    41def material_non_implication(first, second):
    42    if (first, second) == (True, True): return False
    43    return first, second
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    
  • I add another if statement

    41def material_non_implication(first, second):
    42    if (first, second) == (True, True): return False
    43    if (first, second) == (False, True): return False
    44    return first, second
    

    the terminal shows AssertionError

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

    41def material_non_implication(first, second):
    42    if (first, second) == (True, True): return False
    43    if (first, second) == (False, True): return False
    44    if (first, second) == (False, False): return False
    45    return first, second
    

    the terminal shows AttributeError

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

    41def material_non_implication(first, second):
    42    if (first, second) == (True, True): return False
    43    if (first, second) == (False, True): return False
    44    if (first, second) == (False, False): return False
    45    return first, second
    46
    47
    48def material_implication(first, second):
    49    return first, second
    

    the terminal shows AssertionError

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

    48def material_implication(first, second):
    49    if (first, second) == (True, False): return False
    50    return first, second
    

    the terminal shows AttributeError

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

    48def material_implication(first, second):
    49    if (first, second) == (True, False): return False
    50    return first, second
    51
    52
    53def logical_nor(first, second):
    54    return first, second
    

    the terminal shows AssertionError

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

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

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    
  • I add another if statement

    53def logical_nor(first, second):
    54    if (first, second) == (True, True): return False
    55    if (first, second) == (True, False): return False
    56    return first, second
    

    the terminal shows AssertionError

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

    53def logical_nor(first, second):
    54    if (first, second) == (True, True): return False
    55    if (first, second) == (True, False): return False
    56    if (first, second) == (False, True): return False
    57    return first, second
    

    the terminal shows AttributeError

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

    53def logical_nor(first, second):
    54    if (first, second) == (True, True): return False
    55    if (first, second) == (True, False): return False
    56    if (first, second) == (False, True): return False
    57    return first, second
    58
    59
    60def logical_nand(first, second):
    61    return first, second
    

    the terminal shows AssertionError

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

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

    the terminal shows AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'logical_equality'
    
  • I add the function

    76def logical_nand(first, second):
    77    if (first, second) == (True, True): return False
    78    return first, second
    79
    80
    81def logical_equality(first, second):
    82    return first, second
    

    the terminal shows AssertionError

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

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

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    
  • I add another if statement

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

    the terminal shows AttributeError

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

    65def logical_equality(first, second):
    66    if (first, second) == (True, False): return False
    67    if (first, second) == (False, True): return False
    68    return first, second
    69
    70
    71def logical_disjunction(first, second):
    72    return first, second
    

    the terminal shows AssertionError

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

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

    the terminal shows AttributeError

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

    71def logical_disjunction(first, second):
    72    if (first, second) == (False, False): return False
    73    return first, second
    74
    75
    76def logical_conjunction(first, second):
    77    return first, second
    

    the terminal shows AssertionError

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

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

    the terminal shows AssertionError

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

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

    the terminal shows AssertionError

    AssertionError: (False, False) is not false
    
  • I add another if statement

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

    the terminal shows AttributeError

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

    76def logical_conjunction(first, second):
    77    if (first, second) == (True, False): return False
    78    if (first, second) == (False, True): return False
    79    if (first, second) == (False, False): return False
    80    return first, second
    81
    82
    83def exclusive_disjunction(first, second):
    84    return first, second
    

    the terminal shows AssertionError

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

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

    the terminal shows AssertionError

    AssertionError: (False, False) is not false
    
  • I add another if statement

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

    the terminal shows AttributeError

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

    83def exclusive_disjunction(first, second):
    84    if (first, second) == (True, True): return False
    85    if (first, second) == (False, False): return False
    86    return first, second
    87
    88
    89def converse_non_implication(first, second):
    90    return first, second
    

    the terminal shows AssertionError

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

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

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    
  • I add another if statement

    89def converse_non_implication(first, second):
    90    if (first, second) == (True, True): return False
    91    if (first, second) == (True, False): return False
    92    return first, second
    

    the terminal shows AssertionError

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

    89def converse_non_implication(first, second):
    90    if (first, second) == (True, True): return False
    91    if (first, second) == (True, False): return False
    92    if (first, second) == (False, False): return False
    93    return first, second
    

    the terminal shows AttributeError

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

    89def converse_non_implication(first, second):
    90    if (first, second) == (True, True): return False
    91    if (first, second) == (True, False): return False
    92    if (first, second) == (False, False): return False
    93    return first, second
    94
    95
    96def converse_implication(first, second):
    97    return first, second
    

    the terminal shows AssertionError

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

    96def converse_implication(first, second):
    97    if (first, second) == (False, True): return False
    98    return first, second
    

    the terminal shows AttributeError

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

     96def converse_implication(first, second):
     97    if (first, second) == (False, True): return False
     98    return first, second
     99
    100
    101def contradiction(first, second):
    102    return first, second
    

    the terminal shows AssertionError

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

    101def contradiction(first, second):
    102    if (first, second) == (True, True): return False
    103    return first, second
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    
  • I add another if statement

    101def contradiction(first, second):
    102    if (first, second) == (True, True): return False
    103    if (first, second) == (True, False): return False
    104    return first, second
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    
  • I add another if statement

    101def contradiction(first, second):
    102    if (first, second) == (True, True): return False
    103    if (first, second) == (True, False): return False
    104    if (first, second) == (False, True): return False
    105    return first, second
    

    the terminal shows AssertionError

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

    101def contradiction(first, second):
    102    if (first, second) == (True, True): return False
    103    if (first, second) == (True, False): return False
    104    if (first, second) == (False, True): return False
    105    if (first, second) == (False, False): return False
    106    return first, second
    

    the terminal shows

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

All the tests are passing and the world is a better place than when I started! I am going home. Wait, there is more…


REFACTOR: make it better

I can play with the functions I have to make them simpler and understand why my solutions work, since all the tests are passing.

  • contradiction returns False in 4 cases, with 2 inputs there are only 4 cases. I add a return statement

    101def contradiction(first, second):
    102    return False
    103    if (first, second) == (True, True): return False
    104    if (first, second) == (True, False): return False
    105    if (first, second) == (False, True): return False
    106    if (first, second) == (False, False): return False
    107    return first, second
    

    the test is still green.

  • I remove the other lines in contradiction

    101def contradiction(first, second):
    102    return False
    

  • converse_implication returns False in only one case. I write out the if statement

     96def converse_implication(first, second):
     97    # if (first, second) == (False, True): return False
     98    if first == False and second == True:
     99        return False
    100    return first, second
    

    still green

  • I change the statement with not and True

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

    green

  • I remove == True

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

    still green

  • I use a return statement because if something: return False is the same as return not (something)

     96def converse_implication(first, second):
     97    return not (not first and second)
     98    if not first and second:
     99        return False
    100    return first, second
    

    the test is still green

  • I remove the other statements then “multiply not” by the things in the parentheses

    96def converse_implication(first, second):
    97    return (not not first) (not and) (not second)
    98    return not (not first and second)
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    
  • I change not and to or

    96def converse_implication(first, second):
    97    return (not not first) or (not second)
    98    return not (not first and second)
    

    the test is green again

  • I remove the other return statement and not not because two nots make a right?

    96def converse_implication(first, second):
    97    return first or not second
    98    return (not not first) or (not second)
    

    the test is still green

  • I remove the other statement in converse_implication

     96def converse_implication(first, second):
     97    return first or not second
     98
     99
    100def contradiction(first, second):
    101    return False
    

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

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

    the test is still green

  • I break the statement up then add an else clause for the other 3 statements

    89def converse_non_implication(first, second):
    90    # if (first, second) == (False, True): return True
    91    if first == False and second == True:
    92        return True
    93    else:
    94        return False
    95    if (first, second) == (True, True): return False
    96    if (first, second) == (True, False): return False
    97    if (first, second) == (False, False): return False
    98    return first, second
    

    still green

  • I remove the commented line and other statements, then change the if statement with True

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

    green

  • I remove == True

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

    still green

  • I add a return statement because if something: return True is the same as return something

    89def converse_non_implication(first, second):
    90    return not first and second
    91    if not first and second:
    92        return True
    93    else:
    94        return False
    

    the test is still green

  • I remove the other statements in converse_non_implication

    89def converse_non_implication(first, second):
    90    return not first and second
    91
    92
    93def converse_implication(first, second):
    94    return first or not second
    

  • I make the if statements in exclusive_disjunction simpler

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

    the test is still green

  • I remove the first two if statements then change the new second if statement with not and True

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

    still green

  • I remove == True

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

    green

  • I write everything in the second if statement with not because it happens two times in the line

    83def exclusive_disjunction(first, second):
    84    if first and second:
    85        return False
    86    # if not first and not second:
    87    if (not first) (not or) (not second):
    88        return False
    89    return first, second
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    
  • I factor out the nots

    83def exclusive_disjunction(first, second):
    84    if first and second:
    85        return False
    86    # if not first and not second:
    87    # if (not first) (not or) (not second):
    88    if not (first or second):
    89        return False
    90    return first, second
    

    the test is green again

  • I put the two if statements together to make one

    83def exclusive_disjunction(first, second):
    84    # if first and second:
    85    #     return False
    86    # if not (first or second):
    87    if (first and second) or (not (first or second)):
    88        return False
    89    return first, second
    

    the test is still green

  • I remove the commented lines, then add a return statement because if something: return False is the same as return not (something)

    83def exclusive_disjunction(first, second):
    84    return not (
    85        (first and second)
    86        or
    87        (not (first or second))
    88    )
    89    if (first and second) or (not (first or second)):
    90        return False
    91    return first, second
    

    still green

  • I remove the other statements then multiply not by everything in the parentheses because it happens two times in the line

    83def exclusive_disjunction(first, second):
    84    return (
    85        (not (first and second))
    86        (not or)
    87        (not (not (first or second)))
    88    )
    89    return not (
    90        (first and second)
    91        or
    92        (not (first or second))
    93    )
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    
  • I change not or to and

    83def exclusive_disjunction(first, second):
    84    return (
    85        (not (first and second))
    86        and
    87        (not (not (first or second)))
    88    )
    89    return not (
    90        (first and second)
    91        or
    92        (not (first or second))
    93    )
    

    the test is green again

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

    83def exclusive_disjunction(first, second):
    84    return ((not (first and second)) and (first or second))
    85    return not (
    86        (first and second)
    87        or
    88        (not (first or second))
    89    )
    

    green

  • I remove the other statements in exclusive_disjunction

    83def exclusive_disjunction(first, second):
    84    return (not (first and second)) and (first or second)
    85
    86
    87def converse_non_implication(first, second):
    88    return not first and second
    

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

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

    the test is still green

  • I remove the other statements and break up the if statement to make it simpler

    76def logical_conjunction(first, second):
    77    # if (first, second) == (True, True): return True
    78    if first == True and second == True:
    79        return True
    

    the test is still green because all functions return None by default, as if they have an invisible line that says return None

  • I remove == True

    76def logical_conjunction(first, second):
    77    # if first == True and second == True:
    78    if first and second:
    79        return True
    

    still green

  • I use a return statement because if something: return True is the same as return something

    76def logical_conjunction(first, second):
    77    return first and second
    78    if first and second:
    79        return True
    

    green

  • I remove the other statements in logical_conjunction

    76def logical_conjunction(first, second):
    77    return first and second
    78
    79
    80def exclusive_disjunction(first, second):
    81    return (not (first and second)) and (first or second)
    

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

    71def logical_disjunction(first, second):
    72    # if (first, second) == (False, False): return False
    73    if first == False and second == False:
    74        return False
    75    return first, second
    

    the test is still green

  • I remove the comment then change the if statement with not and True

    71def logical_disjunction(first, second):
    72    # if first == False and second == False:
    73    if not first == True and not second == True:
    74        return False
    75    return first, second
    

    still green

  • I remove == True

    71def logical_disjunction(first, second):
    72    # if not first == True and not second == True:
    73    if not first and not second:
    74        return False
    75    return first, second
    

    green

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

    71def logical_disjunction(first, second):
    72    # if not first and not second:
    73    if (not first) (not and) (not second):
    74        return False
    75    return first, second
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    
  • I factor out not

    71def logical_disjunction(first, second):
    72    # if not first and not second:
    73    # if (not first) (not or) (not second):
    74    if not (first or second):
    75        return False
    76    return first, second
    

    the test is green again

  • I add a return statement for the opposite of the if statement because if something: return False is the same as return not (something)

    71def logical_disjunction(first, second):
    72    return not (not (first or second))
    73    if not (first or second):
    74        return False
    75    return first, second
    

    still green

  • I remove not not

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

    the test is still green

  • I remove the other statement in logical_disjunction

    71def logical_disjunction(first, second):
    72    return first or second
    73
    74
    75def logical_conjunction(first, second):
    76    return first and second
    

  • I break up the if statements in logical_equality

    65def logical_equality(first, second):
    66    # if (first, second) == (True, False): return False
    67    if first == True and second == False:
    68        return False
    69    # if (first, second) == (False, True): return False
    70    if first == False and second == True:
    71        return False
    72    return first, second
    

    the test is still green

  • I remove the comments then change the if statements with not and True

    65def logical_equality(first, second):
    66    # if first == True and second == False:
    67    if first == True and not second == True:
    68        return False
    69    # if first == False and second == True:
    70    if not first == True and second == True:
    71        return False
    72    return first, second
    

    still green

  • I remove == True

    65def logical_equality(first, second):
    66    # if first == True and not second == True:
    67    if first and not second:
    68        return False
    69    # if not first == True and second == True:
    70    if not first and second:
    71        return False
    72    return first, second
    

    green

  • I put the two if statements together to make one

    65def logical_equality(first, second):
    66    # if first and not second:
    67    #     return False
    68    # if not first and second:
    69    if (first and not second) or (not first and second):
    70        return False
    71    return first, second
    

    still green

  • I use a return statement because …

    65def logical_equality(first, second):
    66    return not (
    67        (first and not second)
    68        or
    69        (not first and second)
    70    )
    71    if (first and not second) or (not first and second):
    72        return False
    73    return first, second
    

    the test is still green

  • I multiply not by all the symbols in parentheses

    65def logical_equality(first, second):
    66    return (
    67        (not (first and not second))
    68        (not or)
    69        (not (not first and second))
    70    )
    71    return not (
    72        (first and not second)
    73        or
    74        (not first and second)
    75    )
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    
  • I change not or to and

    65def logical_equality(first, second):
    66    return (
    67        (not (first and not second))
    68        and
    69        (not (not first and second))
    70    )
    71    return not (
    72        (first and not second)
    73        or
    74        (not first and second)
    75    )
    

    the test is green again

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

    65def logical_equality(first, second):
    66    return (
    67        # (not (first and not second))
    68        ((not first) (not and) (not not second))
    69        and
    70        (not (not first and second))
    71    )
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    
  • I change not and to or

    65def logical_equality(first, second):
    66    return (
    67        # (not (first and not second))
    68        ((not first) or (not not second))
    69        and
    70        (not (not first and second))
    71    )
    

    the test is green again

  • I remove not not

    65def logical_equality(first, second):
    66    return (
    67        # ((not first) or (not not second))
    68        (not first or second)
    69        and
    70        (not (not first and second))
    71    )
    

    still green

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

    65def logical_equality(first, second):
    66    return (
    67        (not first or second)
    68        and
    69        # (not (not first and second))
    70        ((not not first) (not and) (not second))
    71    )
    

    the terminal shows SyntaxError

    65SyntaxError: invalid syntax
    
  • I change not and to or

    65def logical_equality(first, second):
    66    return (
    67        (not first or second)
    68        and
    69        # (not (not first and second))
    70        ((not not first) or (not second))
    71    )
    

    the test is still green

  • I remove not not

    65def logical_equality(first, second):
    66    return (
    67        (not first or second)
    68        and
    69        # ((not not first) or (not second))
    70        (first or not second)
    71    )
    

    still green

  • I remove the comment

    65def logical_equality(first, second):
    66    return (not first or second) and (first or not second)
    67
    68
    69def logical_disjunction(first, second):
    70    return first or second
    

  • I make the if statement in logical_nand simpler

    60def logical_nand(first, second):
    61    # if (first, second) == (True, True): return False
    62    if first == True and second == True:
    63        return False
    64    return first, second
    

    the test is still green

  • I remove == True

    60def logical_nand(first, second):
    61    # if first == True and second == True:
    62    if first and second:
    63        return False
    64    return first, second
    

    still green

  • I use I use a return statement because …

    60def logical_nand(first, second):
    61    return not (first and second)
    62    if first and second:
    63        return False
    64    return first, second
    

    green

  • I remove the other lines in logical_nand

    60def logical_nand(first, second):
    61    return not (first and second)
    62
    63
    64def logical_equality(first, second):
    65    return (not first or second) and (first or not second)
    

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

    53def logical_nor(first, second):
    54    if first == False and second == False:
    55        return True
    56    else:
    57        return False
    58    if (first, second) == (True, True): return False
    59    if (first, second) == (True, False): return False
    60    if (first, second) == (False, True): return False
    61    return first, second
    

    the test is still green

  • I remove the other lines, then use not and remove == False

    53def logical_nor(first, second):
    54    # if first == False and second == False:
    55    if not first and not second:
    56        return True
    57    else:
    58        return False
    

    still green

  • I use a return statement

    53def logical_nor(first, second):
    54    return not first and not second
    55    if not first and not second:
    56        return True
    57    else:
    58        return False
    

    green

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

    53def logical_nor(first, second):
    54    return (not first) (not or) (not second)
    55    return not first and not second
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    
  • I factor out the not

    53def logical_nor(first, second):
    54    return not (first or second)
    55    # return (not first) (not or) (not second)
    56    return not first and not second
    

    the test is green again

  • I remove the other statements in logical_nor

    53def logical_nor(first, second):
    54    return not (first or second)
    55
    56
    57def logical_nand(first, second):
    58    return not (first and second)
    

  • material_implication has only one case that returns False, I add a return statement for it because if something: return False is the same as return not (something)

    48def material_implication(first, second):
    49    return not ((first, second) == (True, False))
    50    if (first, second) == (True, False): return False
    51    return first, second
    

    the test is still green

  • I break up the return statement to make it simpler

    48def material_implication(first, second):
    49    return not (first == True and second == False)
    50    return not ((first, second) == (True, False))
    

    still green

  • I use not and remove == True and == False

    48def material_implication(first, second):
    49    return not (first and not second)
    50    return not (first == True and second == False)
    

    green

  • I multiply not by everything in the parentheses

    48def material_implication(first, second):
    49    return (not first) (not and) (not not second)
    50    return not (first and not second)
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    
  • I change not and to or

    48def material_implication(first, second):
    49    return (not first) or (not not second)
    50    return not (first and not second)
    

    the test is green again

  • I remove not not

    48def material_implication(first, second):
    49    return not first or second
    50    return (not first) or (not not second)
    51    return not (first and not second)
    

    still green

  • I remove the other statements in material_implication

    48def material_implication(first, second):
    49    return not first or second
    50
    51
    52def logical_nor(first, second):
    53    return not (first or second)
    

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

    41def material_non_implication(first, second):
    42    return (first, second) == (True, False)
    43    if (first, second) == (True, True): return False
    44    if (first, second) == (False, True): return False
    45    if (first, second) == (False, False): return False
    46    return first, second
    

    the test is still green

  • I make the return statement simpler

    41def material_non_implication(first, second):
    42    return first == True and second == False
    43    return (first, second) == (True, False)
    

    still green

  • I use not and remove == True and == False

    41def material_non_implication(first, second):
    42    return first and not second
    43    return first == True and second == False
    

    green

  • I remove the other statement in material_non_implication

    41def material_non_implication(first, second):
    42    return first and not second
    43
    44
    45def material_implication(first, second):
    46    return not first or second
    

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

    35def negate_first(first, second):
    36    if first == True: return False
    37    if (first, second) == (True, True): return False
    38    if (first, second) == (True, False): return False
    39    return first, second
    

    the test is still green

  • I remove == True

    35def negate_first(first, second):
    36    # if first == True: return False
    37    if first: return False
    38    if (first, second) == (True, True): return False
    39    if (first, second) == (True, False): return False
    40    return first, second
    

    still green

  • I add a return statement because …

    35def negate_first(first, second):
    36    return not first
    37    if first: return False
    38    if (first, second) == (True, True): return False
    39    if (first, second) == (True, False): return False
    40    return first, second
    

    green

  • I remove the other statements from negate_first

    35def negate_first(first, second):
    36    return not first
    37
    38
    39def material_non_implication(first, second):
    40    return first and not second
    

  • second is True in the 2 cases where negate_second returns False. I add a return statement if something: return False is the same as …

    29def negate_second(first, second):
    30    return not second == True
    31    if (first, second) == (True, True): return False
    32    if (first, second) == (False, True): return False
    33    return first, second
    

    the test is still green

  • I remove == True

    29def negate_second(first, second):
    30    return not second
    31    return not second == True
    

    green

  • I remove the other statement in negate_second

     1def logical_negation(something):
     2    return not something
     3
     4
     5def logical_identity(something):
     6    return something
     7
     8
     9def logical_true():
    10    return True
    11
    12
    13def logical_false():
    14    return None
    15
    16
    17def tautology(first, second):
    18    return True
    19
    20
    21def project_second(first, second):
    22    return second
    23
    24
    25def project_first(first, second):
    26    return first
    27
    28
    29def negate_second(first, second):
    30    return not second
    31
    32
    33def negate_first(first, second):
    34    return not first
    35
    36
    37def material_non_implication(first, second):
    38    return first and not second
    39
    40
    41def material_implication(first, second):
    42    return not first or second
    43
    44
    45def logical_nor(first, second):
    46    return not (first or second)
    47
    48
    49def logical_nand(first, second):
    50    return not (first and second)
    51
    52
    53def logical_equality(first, second):
    54    return (not first or second) and (first or not second)
    55
    56
    57def logical_disjunction(first, second):
    58    return first or second
    59
    60
    61def logical_conjunction(first, second):
    62    return first and second
    63
    64
    65def exclusive_disjunction(first, second):
    66    return (not (first and second)) and (first or second)
    67
    68
    69def converse_non_implication(first, second):
    70    return not first and second
    71
    72
    73def converse_implication(first, second):
    74    return first or not second
    75
    76
    77def contradiction(first, second):
    78    return False
    
  • all the other functions are already simple


close the project

  • I close truth_table.py in the editor

  • I click in the terminal, then 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 True or False for the operations of the Truth Table from Mathematics

and

All the logic statements 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?

you now know

Would you like to test making a calculator?


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