truth table: test_truth_table_tests


requirements

how to get back to the automated tests

If your tests stopped after the previous chapter, heres’s how to get back to the tests


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

RED: make it fail

  • I close test_truth_table.py

  • then 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 'tautology'
    

GREEN: make it pass

  • I add the name to truth_table.py

    1tautology
    

    the terminal shows NameError

    NameError: name 'tautology' is not defined
    

    I point the name to None

    1tautology = None
    

    the terminal shows TypeError

    TypeError: 'NoneType' object is not callable
    

    I make tautology a function

    1def tautology():
    2    return None
    

    the terminal shows TypeError

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

    I add names in parentheses for the function to take input arguments

    1def tautology(first_input, second_input):
    2    return None
    

    the terminal shows AssertionError

    AssertionError: None is not true
    

    I change None to True in the return statement

    1def tautology(first_input, second_input):
    2    return True
    

    the terminal shows AttributeError

    AttributeError: module 'src.truth_table' has no attribute 'project_second'
    
  • I add the function using the solution I just used for tautology

    1def tautology(first_input, second_input):
    2    return True
    3
    4
    5def project_second(first_input, second_input):
    6    return True
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    I change the return statement

    5def project_second(first_input, second_input):
    6    return False
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    The expectation of the test changes. I want to see the difference between the inputs and the expected output

    5def project_second(first_input, second_input):
    6    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    second_input is False, I remove first_input from the return statement

    5def project_second(first_input, second_input):
    6    return second_input
    

    the terminal shows AttributeError

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

     5def project_second(first_input, second_input):
     6    return second_input
     7
     8
     9def project_first(first_input, second_input):
    10    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    first_input is False

  • I remove second_input from the return statement

     9def project_first(first_input, second_input):
    10    return first_input
    

    the terminal shows AttributeError

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

     9def project_first(first_input, second_input):
    10    return first_input
    11
    12
    13def negate_second(first_input, second_input):
    14    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, True) is not false
    

    I add an if statement to negate_second

    13def negate_second(first_input, second_input):
    14    if (first_input, second_input) == (True, True): return False
    15    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    I add another if statement

    13def negate_second(first_input, second_input):
    14    if (first_input, second_input) == (True, True): return False
    15    if (first_input, second_input) == (False, True): return False
    16    return first_input, second_input
    

    the terminal shows AttributeError

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

    13def negate_second(first_input, second_input):
    14    if (first_input, second_input) == (True, True): return False
    15    if (first_input, second_input) == (False, True): return False
    16    return first_input, second_input
    17
    18
    19def negate_first(first_input, second_input):
    20    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, True)
    

    I add an if statement

    19def negate_first(first_input, second_input):
    20    if (first_input, second_input) == (True, True): return False
    21    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    I add an if statement

    19def negate_first(first_input, second_input):
    20    if (first_input, second_input) == (True, True): return False
    21    if (first_input, second_input) == (True, False): return False
    22    return first_input, second_input
    

    the terminal shows AttributeError

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

    21    if (first_input, second_input) == (True, False): return False
    22    return first_input, second_input
    23
    24
    25def material_non_implication(first_input, second_input):
    26    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, True) is not false
    

    I add an if statement

    25def material_non_implication(first_input, second_input):
    26    if (first_input, second_input) == (True, True): return False
    27    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    I add another if statement

    25def material_non_implication(first_input, second_input):
    26    if (first_input, second_input) == (True, True): return False
    27    if (first_input, second_input) == (False, True): return False
    28    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (False, False) is not false
    

    I add an if statement for it

    25def material_non_implication(first_input, second_input):
    26    if (first_input, second_input) == (True, True): return False
    27    if (first_input, second_input) == (False, True): return False
    28    if (first_input, second_input) == (False, False): return False
    29    return first_input, second_input
    

    the terminal shows AttributeError

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

    28    if (first_input, second_input) == (False, False): return False
    29    return first_input, second_input
    30
    31
    32def material_implication(first_input, second_input):
    33    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    I add an if statement

    32def material_implication(first_input, second_input):
    33    if (first_input, second_input) == (True, False): return False
    34    return first_input, second_input
    

    the terminal shows AttributeError

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

    33    if (first_input, second_input) == (True, False): return False
    34    return first_input, second_input
    35
    36
    37def logical_nor(first_input, second_input):
    38    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, True) is not false
    

    I add an if statement

    37def logical_nor(first_input, second_input):
    38    if (first_input, second_input) == (True, True): return False
    39    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    I add another if statement

    37def logical_nor(first_input, second_input):
    38    if (first_input, second_input) == (True, True): return False
    39    if (first_input, second_input) == (True, False): return False
    40    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    I add an if statement

    37def logical_nor(first_input, second_input):
    38    if (first_input, second_input) == (True, True): return False
    39    if (first_input, second_input) == (True, False): return False
    40    if (first_input, second_input) == (False, True): return False
    41    return first_input, second_input
    

    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

    40    if (first_input, second_input) == (False, True): return False
    41    return first_input, second_input
    42
    43
    44def logical_nand(first_input, second_input):
    45    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, True) is not false
    

    I add an if statement

    44def logical_nand(first_input, second_input):
    45    if (first_input, second_input) == (True, True): return False
    46    return first_input, second_input
    

    the terminal shows AttributeError

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

    45    if (first_input, second_input) == (True, True): return False
    46    return first_input, second_input
    47
    48
    49def logical_equality(first_input, second_input):
    50    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    I add an if statement

    49def logical_equality(first_input, second_input):
    50    if (first_input, second_input) == (True, False): return False
    51    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    I add another if statement

    49def logical_equality(first_input, second_input):
    50    if (first_input, second_input) == (True, False): return False
    51    if (first_input, second_input) == (False, True): return False
    52    return first_input, second_input
    

    the terminal shows AttributeError

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

    51    if (first_input, second_input) == (False, True): return False
    52    return first_input, second_input
    53
    54
    55def logical_disjunction(first_input, second_input):
    56    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (False, False) is not false
    

    I add an if statement

    55def logical_disjunction(first_input, second_input):
    56    if (first_input, second_input) == (False, False): return False
    57    return first_input, second_input
    

    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

    56    if (first_input, second_input) == (False, False): return False
    57    return first_input, second_input
    58
    59
    60def logical_conjunction(first_input, second_input):
    61    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    I add an if statement

    60def logical_conjunction(first_input, second_input):
    61    if (first_input, second_input) == (True, False): return False
    62    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    I add an if statement

    60def logical_conjunction(first_input, second_input):
    61    if (first_input, second_input) == (True, False): return False
    62    if (first_input, second_input) == (False, True): return False
    63    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (False, False) is not false
    

    I add another if statement

    60def logical_conjunction(first_input, second_input):
    61    if (first_input, second_input) == (True, False): return False
    62    if (first_input, second_input) == (False, True): return False
    63    if (first_input, second_input) == (False, False): return False
    64    return first_input, second_input
    

    the terminal shows AttributeError

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

    63    if (first_input, second_input) == (False, False): return False
    64    return first_input, second_input
    65
    66
    67def exclusive_disjunction(first_input, second_input):
    68    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, True) is not false
    

    I add an if statement

    67def exclusive_disjunction(first_input, second_input):
    68    if (first_input, second_input) == (True, True): return False
    69    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (False, False) is not false
    

    I add another if statement

    67def exclusive_disjunction(first_input, second_input):
    68    if (first_input, second_input) == (True, True): return False
    69    if (first_input, second_input) == (False, False): return False
    70    return first_input, second_input
    

    the terminal shows AttributeError

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

    69    if (first_input, second_input) == (False, False): return False
    70    return first_input, second_input
    71
    72
    73def converse_non_implication(first_input, second_input):
    74    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, True) is not false
    

    I add an if statement

    73def converse_non_implication(first_input, second_input):
    74    if (first_input, second_input) == (True, True): return False
    75    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    I add another if statement

    73def converse_non_implication(first_input, second_input):
    74    if (first_input, second_input) == (True, True): return False
    75    if (first_input, second_input) == (True, False): return False
    76    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (False, False) is not false
    

    I add an if statement

    73def converse_non_implication(first_input, second_input):
    74    if (first_input, second_input) == (True, True): return False
    75    if (first_input, second_input) == (True, False): return False
    76    if (first_input, second_input) == (False, False): return False
    77    return first_input, second_input
    

    the terminal shows AttributeError

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

    76    if (first_input, second_input) == (False, False): return False
    77    return first_input, second_input
    78
    79
    80def converse_implication(first_input, second_input):
    81    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    I add an if statement

    80def converse_implication(first_input, second_input):
    81    if (first_input, second_input) == (False, True): return False
    82    return first_input, second_input
    

    the terminal shows AttributeError

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

    81    if (first_input, second_input) == (False, True): return False
    82    return first_input, second_input
    83
    84
    85def contradiction(first_input, second_input):
    86    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, True) is not false
    

    I add an if statement

    85def contradiction(first_input, second_input):
    86    if (first_input, second_input) == (True, True): return False
    87    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (True, False) is not false
    

    I add another if statement

    85def contradiction(first_input, second_input):
    86    if (first_input, second_input) == (True, True): return False
    87    if (first_input, second_input) == (True, False): return False
    88    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (False, True) is not false
    

    I add another if statement

    85def contradiction(first_input, second_input):
    86    if (first_input, second_input) == (True, True): return False
    87    if (first_input, second_input) == (True, False): return False
    88    if (first_input, second_input) == (False, True): return False
    89    return first_input, second_input
    

    the terminal shows AssertionError

    AssertionError: (False, False) is not false
    

    I add an if statement

    85def contradiction(first_input, second_input):
    86    if (first_input, second_input) == (True, True): return False
    87    if (first_input, second_input) == (True, False): return False
    88    if (first_input, second_input) == (False, True): return False
    89    if (first_input, second_input) == (False, False): return False
    90    return first_input, second_input
    

    the terminal shows AttributeError

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

    89    if (first_input, second_input) == (False, False): return False
    90    return first_input, second_input
    91
    92
    93def logical_negation(first_input, second_input):
    94    return first_input, second_input
    

    the terminal shows TypeError

    TypeError: logical_negation() missing 1 required positional argument: 'second_input'
    
  • I remove second_input from the parentheses, to make the function take only 1 input,

    93def logical_negation(first_input):
    94    return first_input
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    I add “not” to the return statement

    93def logical_negation(first_input):
    94    return not first_input
    

    the terminal shows AttributeError

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

    93def logical_negation(first_input):
    94    return not first_input
    95
    96
    97def logical_identity(the_input):
    98    return not the_input
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    I remove “not” from the return statement

    97def logical_identity(the_input):
    98    return the_input
    

    the terminal shows AttributeError

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

     97def logical_identity(the_input):
     98    return the_input
     99
    100
    101def logical_true(the_input):
    102    return the_input
    

    the terminal shows TypeError

    TypeError: logical_true() missing 1 required positional argument: 'x'
    

    I remove first_input from the parentheses, this function does not take input, I change the return statement

    101def logical_true():
    102    return None
    

    the terminal shows AssertionError

    AssertionError: None is not true
    

    I change None to True in the return statement

    101def logical_true():
    102    return True
    

    the terminal shows AttributeError

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

    101def logical_true():
    102    return True
    103
    104
    105def logical_false():
    106    return False
    

    the terminal shows green! All tests are passing and the world is a better place than when I started.

REFACTOR: make it better

I can refactor the functions I have, to make them simpler since all the tests are passing

  • logical_false, logical_true, logical_identity and logical_negation are already simple

  • I change the name of the input in Logical Negation

    Tip

    In Visual Studio Code I can change everywhere a name is by using

    • Find and Replace - ctrl+H on Windows or option+command+F on MacOS

    • Rename Symbol

      • Right click on the name you want to change, for example first_input then select Rename Symbol or

      • Select the name you want to change then hit F2 to rename it

    93def logical_negation(the_input):
    94    return not the_input
    
  • contradiction returns False in 4 cases, with 2 inputs there are only 4 cases. I add a return statement

    85def contradiction(first_input, second_input):
    86    return False
    87    if (first_input, second_input) == (True, True): return False
    88    if (first_input, second_input) == (True, False): return False
    89    if (first_input, second_input) == (False, True): return False
    90    if (first_input, second_input) == (False, False): return False
    91    return first_input, second_input
    

    the test is still green. I remove the other lines in the function

    85def contradiction(first_input, second_input):
    86    return False
    87
    88
    89def logical_negation(x):
    90    return not first_input
    
  • converse_implication returns False in only one case, I return the logical negation of the if statement, for the 3 cases that return True

    80def converse_implication(first_input, second_input):
    81    return (first_input, second_input) != (False, True)
    82    if (first_input, second_input) == (False, True): return False
    83    return first_input, second_input
    

    still green. I remove the other statements

    80def converse_implication(first_input, second_input):
    81    return (first_input, second_input) != (False, True)
    82
    83
    84def contradiction(first_input, second_input):
    85    return False
    
  • converse_non_implication has only one case that returns True, it is the missing case. I add a return statement for it

    73def converse_non_implication(first_input, second_input):
    74    return (first_input, second_input) == (False, True)
    75    if (first_input, second_input) == (True, True): return False
    76    if (first_input, second_input) == (True, False): return False
    77    if (first_input, second_input) == (False, False): return False
    78    return first_input, second_input
    

    the terminal still shows green and I remove the other lines

    73def converse_non_implication(first_input, second_input):
    74    return (first_input, second_input) == (False, True)
    75
    76
    77def converse_implication(first_input, second_input):
    78    return (first_input, second_input) != (False, True)
    
  • exclusive_disjunction has two if statements. I put them together to make one if statement

    67def exclusive_disjunction(first_input, second_input):
    68    if (
    69        (first_input, second_input) == (True, True)
    70        or
    71        (first_input, second_input) == (False, False)
    72    ): return False
    73    if (first_input, second_input) == (True, True): return False
    74    if (first_input, second_input) == (False, False): return False
    75    return first_input, second_input
    

    the test is still green. I remove the other if statements then return the logical negation of the if statement

    67def exclusive_disjunction(first_input, second_input):
    68    return (
    69        (first_input, second_input) != (True, True)
    70        and
    71        (first_input, second_input) != (False, False)
    72    )
    73
    74
    75def converse_non_implication(first_input, second_input):
    76    return (first_input, second_input) == (False, True)
    

    the test is still green, I remove the other lines

    67def exclusive_disjunction(first_input, second_input):
    68    return (first_input, second_input) != (True, True) and return (first_input, second_input) != (False, False)
    69
    70
    71def converse_non_implication(first_input, second_input):
    72    return (first_input, second_input) == (False, True)
    
  • logical_conjunction only has one case that returns True, it is the missing case. I add a return statement for it

    60def logical_conjunction(first_input, second_input):
    61    return (first_input, second_input) == (True, True)
    62    if (first_input, second_input) == (True, False): return False
    63    if (first_input, second_input) == (False, True): return False
    64    if (first_input, second_input) == (False, False): return False
    65    return first_input, second_input
    

    still green, I remove the other statements

    60def logical_conjunction(first_input, second_input):
    61    return (first_input, second_input) == (True, True)
    62
    63
    64def exclusive_disjunction(first_input, second_input):
    65    return (
    
  • logical_disjunction only has one case that returns False, I add a return statement for the opposite of it which covers the other 3 cases

    55def logical_disjunction(first_input, second_input):
    56    return (first_input, second_input) != (False, False)
    57    if (first_input, second_input) == (False, False): return False
    58    return first_input, second_input
    

    the terminal still shows green. I remove the other lines

    55def logical_disjunction(first_input, second_input):
    56    return (first_input, second_input) != (False, False)
    57
    58
    59def logical_conjunction(first_input, second_input):
    60    return (first_input, second_input) == (True, True)
    
  • logical_equality has two if statements. I use what I know from exclusive_disjunction to add a return statement

    49def logical_equality(first_input, second_input):
    50    return (
    51        (first_input, second_input) != (True, False)
    52        and
    53        (first_input, second_input) != (False, True)
    54    )
    55    if (first_input, second_input) == (True, False): return False
    56    if (first_input, second_input) == (False, True): return False
    57    return first_input, second_input
    

    the terminal still shows green. I remove the other lines

    49def logical_equality(first_input, second_input):
    50    return (
    51        (first_input, second_input) != (True, False)
    52        and
    53        (first_input, second_input) != (False, True)
    54    )
    55
    56
    57def logical_disjunction(first_input, second_input):
    58    return (first_input, second_input) != (False, False)
    
  • logical_nand only has one case that returns False, I add a return statement for its opposite which covers the other 3 cases

    44def logical_nand(first_input, second_input):
    45    return (first_input, second_input) != (True, True)
    46    if (first_input, second_input) == (True, True): return False
    47    return first_input, second_input
    

    still green. I remove the other lines

    44def logical_nand(first_input, second_input):
    45    return (first_input, second_input) != (True, True)
    46
    47
    48def logical_equality(first_input, second_input):
    49    return (
    
  • logical_nor only has one case that returns True, I add a return statement for it

    37def logical_nor(first_input, second_input):
    38    return (first_input, second_input) == (False, False)
    39    if (first_input, second_input) == (True, True): return False
    40    if (first_input, second_input) == (True, False): return False
    41    if (first_input, second_input) == (False, True): return False
    42    return first_input, second_input
    

    still green, I remove the other statements

    37def logical_nor(first_input, second_input):
    38    return (first_input, second_input) == (False, False)
    39
    40
    41def logical_nand(first_input, second_input):
    42    return (first_input, second_input) != (True, True)
    
  • material_implication has only one case that returns False, I add a return statement for the other 3

    32def material_implication(first_input, second_input):
    33    return (first_input, second_input) != (True, False)
    34    if (first_input, second_input) == (True, False): return False
    35    return first_input, second_input
    

    the test is still green. I remove the other statements

    32def material_implication(first_input, second_input):
    33    return (first_input, second_input) != (True, False)
    34
    35
    36def logical_nor(first_input, second_input):
    37    return (first_input, second_input) == (False, False)
    
  • material_non_implication has 3 cases that return False. I add a return statement for the missing case that returns True

    25def material_non_implication(first_input, second_input):
    26    return (first_input, second_input) == (True, False)
    27    if (first_input, second_input) == (True, True): return False
    28    if (first_input, second_input) == (False, True): return False
    29    if (first_input, second_input) == (False, False): return False
    30    return first_input, second_input
    

    the terminal still shows green. I remove the other statements

    25def material_non_implication(first_input, second_input):
    26    return (first_input, second_input) == (True, False)
    27
    28
    29def material_implication(first_input, second_input):
    30    return (first_input, second_input) != (True, False)
    
  • first_input is True in the 2 cases where negate_first returns False, I add an if statement for them

    19def negate_first(first_input, second_input):
    20    if first_input == True: return False
    21    if (first_input, second_input) == (True, True): return False
    22    if (first_input, second_input) == (True, False): return False
    23    return first_input, second_input
    

    the test is still green. I remove the other if statements, then add a simpler return statement

    19def negate_first(first_input, second_input):
    20    return first_input != True
    21    if first_input == True: return False
    22    return first_input, second_input
    

    the test is still green, I remove the other statements

    19def negate_first(first_input, second_input):
    20    return first_input != True
    21
    22
    23def material_non_implication(first_input, second_input):
    24    return (first_input, second_input) == (True, False)
    
  • second_input is True in the 2 cases where negate_second returns False. I add a return statement like the one from negate_first

    13def negate_second(first_input, second_input):
    14    return second_input != True
    15    if (first_input, second_input) == (True, True): return False
    16    if (first_input, second_input) == (False, True): return False
    17    return first_input, second_input
    

    still green. I remove the other statements

    13def negate_second(first_input, second_input):
    14    return second_input != True
    15
    16
    17def negate_first(first_input, second_input):
    18    return first_input != True
    
  • project_second, project_first and tautology are already simple

  • I change the return statement in negate_second to make it simpler

    13def negate_second(first_input, second_input):
    14    return not second_input == True
    15    return second_input != True
    

    when not second_input is True it means the return statement is True == True which is a duplication. I remove the second part of the statement and the second return statement

    13def negate_second(first_input, second_input):
    14    return not second_input
    15
    16
    17def negate_first(first_input, second_input):
    

    the test is still green

  • I do the same thing with negate_first

    17def negate_first(first_input, second_input):
    18    return not first_input
    19    return first_input != True
    

    still green. I remove the second return statement

    17def negate_first(first_input, second_input):
    18    return not first_input
    19
    20
    21def material_non_implication(first_input, second_input):
    
  • I use this with material_non_implication

    21def material_non_implication(first_input, second_input):
    22    return first_input and not second_input
    23    return (first_input, second_input) == (True, False)
    

    the terminal shows all tests are still passing. I remove the second return statement

    21def material_non_implication(first_input, second_input):
    22    return first_input and not second_input
    23
    24
    25def material_implication(first_input, second_input):
    
  • I try it with material_implication

    25def material_implication(first_input, second_input):
    26    return not first_input and second_input
    27    return (first_input, second_input) != (True, False)
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    Not good! I change “and” to “or

    25def material_implication(first_input, second_input):
    26    return not first_input or second_input
    27    return (first_input, second_input) != (True, False)
    

    the test is green again. Note to self - use “or” the next time I see != in these tests. I remove the other return statement

    25def material_implication(first_input, second_input):
    26    return not first_input or second_input
    27
    28
    29def logical_nor(first_input, second_input):
    
  • I do the same thing with logical_nor

    29def logical_nor(first_input, second_input):
    30    return not first_input and not second_input
    31    return (first_input, second_input) == (False, False)
    

    the test is still green. I remove the second return statement and change the first return statement in terms of “not” since it happens 2 times

    29def logical_nor(first_input, second_input):
    30    return (not first_input) (not or) (not second_input)
    31    return not first_input and not second_input
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    

    I comment the line out then factor out “not

    29def logical_nor(first_input, second_input):
    30    return not (first_input or second_input)
    31    # return (not first_input) (not or) (not second_input)
    32    return not first_input and not second_input
    

    the terminal still shows green. I remove the other statements

    29def logical_nor(first_input, second_input):
    30    return not (first_input or second_input)
    31
    32
    33def logical_nand(first_input, second_input):
    
  • I add a return statement to logical_nand

    33def logical_nand(first_input, second_input):
    34    return not first_input or not second_input
    35    return (first_input, second_input) != (True, True)
    

    the test is still green, I remove the second return statement then factor out “not” in the first

    33def logical_nand(first_input, second_input):
    34    return not (first_input and second_input)
    35    return not first_input or not second_input
    

    still green. I remove the other return statements

    33def logical_nand(first_input, second_input):
    34    return not (first_input and second_input)
    35
    36
    37def logical_equality(first_input, second_input):
    
  • I add a return statement to logical_equality

    37def logical_equality(first_input, second_input):
    38    return (not first_input or second_input) and (first_input or not second_input)
    39    return (
    40        (first_input, second_input) != (True, False)
    41        and
    42        (first_input, second_input) != (False, True)
    43    )
    

    the test is still green, I remove the other return statement

    37def logical_equality(first_input, second_input):
    38    return (not first_input or second_input) and (first_input or not second_input)
    39
    40
    41def logical_disjunction(first_input, second_input):
    
  • I do the same thing with logical_disjunction

    41def logical_disjunction(first_input, second_input):
    42    return first_input or second_input
    43    return (first_input, second_input) != (False, False)
    

    still green. I remove the other return statement

    41def logical_disjunction(first_input, second_input):
    42    return first_input or second_input
    43
    44
    45def logical_conjunction(first_input, second_input):
    
  • on to logical_conjunction

    45def logical_conjunction(first_input, second_input):
    46    return first_input and second_input
    47    return (first_input, second_input) == (True, True)
    

    the terminal still shows green. I remove the other return statement

    45def logical_conjunction(first_input, second_input):
    46    return first_input and second_input
    47
    48
    49def exclusive_disjunction(first_input, second_input):
    
  • I add a return statement to exclusive_disjunction

    49def exclusive_disjunction(first_input, second_input):
    50    return (
    51        (not first_input or not second_input)
    52        and
    53        (first_input or second_input)
    54    )
    55    return (
    56        (first_input, second_input) != (True, True)
    57        and
    58        (first_input, second_input) != (False, False)
    59    )
    

    still green. I remove the second return statement then factor out “not” from the first part of the statement

    49def exclusive_disjunction(first_input, second_input):
    50    return (
    51        not (first_input and second_input)
    52        # (not first_input or not second_input)
    53        and
    54        (first_input or second_input)
    55    )
    

    the test is still green. I remove the commented line

    49def exclusive_disjunction(first_input, second_input):
    50    return (
    51        not (first_input and second_input)
    52        and
    53        (first_input or second_input)
    54    )
    55
    56
    57def converse_non_implication(first_input, second_input):
    
  • I add a simpler return statement to converse_non_implication

    57def converse_non_implication(first_input, second_input):
    58    return not first_input and second_input
    59    return (first_input, second_input) == (False, True)
    

    still green. I remove the other line

    57def converse_non_implication(first_input, second_input):
    58    return not first_input and second_input
    59
    60
    61def converse_implication(first_input, second_input):
    
  • time for converse_implication

    61def converse_implication(first_input, second_input):
    62    return first_input or not second_input
    63    return (first_input, second_input) != (False, True)
    

    I remove the second return statement

    61def converse_implication(first_input, second_input):
    62    return first_input or not second_input
    63
    64
    65def contradiction(first_input, second_input):
    

all the tests are still passing


close the project

  • I close the file(s) I had open in the editor(s)

  • I exit the tests in the terminal with Ctrl+C on the keyboard

  • I deactivate the virtual environment

    deactivate
    

    the terminal goes back to the command line, (.venv) is no longer on the left side

    .../pumping_python/truth_table
    
  • 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?


please leave a review