truth table: Binary Operations part 4


requirements

Binary Operations part 3

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


test_negate_second

RED: make it fail

I add a new test for another Binary Operation to test_truth_table.py with the first case where both first_input and second_input are True

 97        self.assertTrue(src.truth_table.converse_implication(False, False))
 98
 99    def test_negate_second(self):
100        self.assertFalse(src.truth_table.negate_second(True, True))
101
102# Exceptions seen

the terminal shows AttributeError

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

there is no definition for it yet

GREEN: make it pass

I add a function definition for negate_second to truth_table.py

63def converse_implication(first_input, second_input):
64    return first_input or not second_input
65
66
67def negate_second(first_input, second_input):
68    return False

the test passes. negate_second returns False when the two inputs are True

REFACTOR: make it better

  • I add the next case - when first_input is True and second_input is False, to test_negate_second in test_truth_table.py

     99    def test_negate_second(self):
    100        self.assertFalse(src.truth_table.negate_second(True, True))
    101        self.assertTrue(src.truth_table.negate_second(True, False))
    

    the terminal shows AssertionError

    AssertionError: False is not true
    
  • I add an if statement to negate_second in truth_table.py

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

    the test passes. negate_second returns

  • I add the third case - when the first input is False and the second input is True, to test_negate_second in test_truth_table.py

     99    def test_negate_second(self):
    100        self.assertFalse(src.truth_table.negate_second(True, True))
    101        self.assertTrue(src.truth_table.negate_second(True, False))
    102        self.assertFalse(src.truth_table.negate_second(False, True))
    

    the test is still passing. negate_second returns

  • I add the last case - when the two inputs are False

     99    def test_negate_second(self):
    100        self.assertFalse(src.truth_table.negate_second(True, True))
    101        self.assertTrue(src.truth_table.negate_second(True, False))
    102        self.assertFalse(src.truth_table.negate_second(False, True))
    103        self.assertTrue(src.truth_table.negate_second(False, False))
    

    the terminal shows AssertionError

    AssertionError: False is not true
    
  • I add an if statement for the case to negate_second in truth_table.py

    67def negate_second(first_input, second_input):
    68    if first_input == False and second_input == False:
    69        return True
    70    if first_input == True and second_input == False:
    71        return True
    72    return False
    

    the test passes. negate_second returns

  • I add a return statement to show that negate_second always returns the opposite of the second input

    67def negate_second(first_input, second_input):
    68    return not second_input
    69    if first_input == False and second_input == False:
    70        return True
    71    if first_input == True and second_input == False:
    72        return True
    73    return False
    

    the test is still green

  • I remove the other statements

    67def negate_second(first_input, second_input):
    68    return not second_input
    

Negate Second always returns

it is the opposite or Logical Negation of Project Second which always returns the second input


test_logical_nor

RED: make it fail

I add a test for logical_nor in test_truth_table.py

103        self.assertTrue(src.truth_table.negate_second(False, False))
104
105    def test_logical_nor(self):
106        self.assertFalse(src.truth_table.logical_nor(True, True))
107
108
109# Exceptions seen

the terminal shows AttributeError

AttributeError: module 'src.truth_table' has no attribute 'logical_nor'. Did you mean: 'logical_nand'?

GREEN: make it pass

I add the function to truth_table.py

67def negate_second(first_input, second_input):
68    return not second_input
69
70
71def logical_nor(first_input, second_input):
72    return False

the test passes. logical_nor returns False when the first and second inputs are both True

REFACTOR: make it better

  • I add the next case to test_logical_nor in test_truth_table.py - when first_input is True and second_input is False

    105    def test_logical_nor(self):
    106        self.assertFalse(src.truth_table.logical_nor(True, True))
    107        self.assertFalse(src.truth_table.logical_nor(True, False))
    

    the terminal still shows green. logical_nor returns

  • on to the next case - when the first input is False and the second input is True

    105    def test_logical_nor(self):
    106        self.assertFalse(src.truth_table.logical_nor(True, True))
    107        self.assertFalse(src.truth_table.logical_nor(True, False))
    108        self.assertFalse(src.truth_table.logical_nor(False, True))
    

    the test is still green. logical_nor returns

  • I add the last case - when the two inputs are False

    105    def test_logical_nor(self):
    106        self.assertFalse(src.truth_table.logical_nor(True, True))
    107        self.assertFalse(src.truth_table.logical_nor(True, False))
    108        self.assertFalse(src.truth_table.logical_nor(False, True))
    109        self.assertTrue(src.truth_table.logical_nor(False, False))
    110
    111
    112# Exceptions seen
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    this is the only case where logical_nor returns True

  • I add a conditional expression for it in truth_table.py

    71def logical_nor(first_input, second_input):
    72    return True if (first_input == False and second_input == False) else False
    73    return False
    

    the test passes. logical_nor returns

  • I remove the second return statement then use the simpler form of the conditional expression

    71def logical_nor(first_input, second_input):
    72    return first_input == False and second_input == False
    73    return True if (first_input == False and second_input == False) else False
    

    the test is still green

  • I remove the second return statement and write the first one in terms of True

    71def logical_nor(first_input, second_input):
    72    return not first_input == True and not second_input == True
    73    return first_input == False and second_input == False
    

    still green

  • I remove the second return statement and make the first one simpler with bool

    71def logical_nor(first_input, second_input):
    72    return not bool(first_input) and not bool(second_input)
    73    return not first_input == True and not second_input == True
    

    the terminal still shows green

  • I make the statement simpler again

    71def logical_nor(first_input, second_input):
    72    return not first_input and not second_input
    73    return not bool(first_input) and not bool(second_input)
    

    green

  • I remove the second return statement then factor out “not” since it happens 2 times in the first statement

    71def logical_nor(first_input, second_input):
    72    return (not first_input) (not or) (not second_input)
    73    return not first_input and not second_input
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    
  • I comment out the line then add the correct statement

    71def logical_nor(first_input, second_input):
    72    return not (first_input or second_input)
    73    # return (not first_input) (not or) (not second_input)
    74    return not first_input and not second_input
    

    green, green, green again

  • I remove the other statements

    71def logical_nor(first_input, second_input):
    72    return not (first_input or second_input)
    

Logical NOR returns


test_logical_equality

RED: make it fail

I add a new test for the next Binary Operation in test_truth_table.py

109        self.assertTrue(src.truth_table.logical_nor(False, False))
110
111    def test_logical_equality(self):
112        self.assertTrue(src.truth_table.logical_equality(True, True))
113
114
115# Exceptions seen

the terminal shows AttributeError

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

GREEN: make it pass

I add a function definition for it in truth_table.py

71def logical_nor(first_input, second_input):
72    return not (first_input or second_input)
73
74
75def logical_equality(first_input, second_input):
76    return True

the test passes. logical_equality returns True when the two inputs are True

REFACTOR: make it better

  • I add the next case to test_logical_equality in test_truth_table.py

    111    def test_logical_equality(self):
    112        self.assertTrue(src.truth_table.logical_equality(True, True))
    113        self.assertFalse(src.truth_table.logical_equality(True, False))
    

    the terminal shows AssertionError

    AssertionError: True is not false
    
  • I add an if statement to logical_equality in truth_table.py

    75def logical_equality(first_input, second_input):
    76    if first_input and not second_input:
    77        return False
    78    return True
    

    the test passes. logical_equality returns

  • I add the next case to test_logical_equality in test_truth_table.py

    111    def test_logical_equality(self):
    112        self.assertTrue(src.truth_table.logical_equality(True, True))
    113        self.assertFalse(src.truth_table.logical_equality(True, False))
    114        self.assertFalse(src.truth_table.logical_equality(False, True))
    

    the terminal shows AssertionError

    AssertionError: True is not false
    
  • I add another if statement to logical_equality in truth_table.py

    75def logical_equality(first_input, second_input):
    76    if not first_input and second_input:
    77        return False
    78    if first_input and not second_input:
    79        return False
    80    return True
    

    the test passes. logical_equality returns

  • I add the last case to test_logical_equality in test_truth_table.py

    111    def test_logical_equality(self):
    112        self.assertTrue(src.truth_table.logical_equality(True, True))
    113        self.assertFalse(src.truth_table.logical_equality(True, False))
    114        self.assertFalse(src.truth_table.logical_equality(False, True))
    115        self.assertTrue(src.truth_table.logical_equality(False, False))
    116
    117
    118# Exceptions seen
    

    the test is still green. logical_equality returns

  • I use “or” to put the 2 statements that return False together, since they are at the same level in logical_equality in truth_table.py

    75def logical_equality(first_input, second_input):
    76    if (not first_input and second_input) or (first_input and not second_input):
    77        return False
    78    # if not first_input and second_input:
    79    #     return False
    80    # if first_input and not second_input:
    81    #     return False
    82    return True
    

    still green

  • I remove the commented lines

    75def logical_equality(first_input, second_input):
    76    if (not first_input and second_input) or (first_input and not second_input):
    77        return False
    78    return True
    

    the test is still passing

  • I write a new return statement with not to replace the if statement

    75def logical_equality(first_input, second_input):
    76    return not ((not first_input and second_input) or (first_input and not second_input))
    77    if (not first_input and second_input) or (first_input and not second_input):
    78        return False
    79    return True
    

    the test is still green

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

    75def logical_equality(first_input, second_input):
    76    return (
    77        (not (not first_input and second_input))
    78        (not or)
    79        (not (first_input and not second_input))
    80    )
    81    return not ((not first_input and second_input) or (first_input and not second_input))
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    

    I change “not or” to “and

    75def logical_equality(first_input, second_input):
    76    return (
    77        (not (not first_input and second_input))
    78        and
    79        (not (first_input and not second_input))
    80    )
    81    return not ((not first_input and second_input) or (first_input and not second_input))
    

    the test is green again

  • I remove the other return statement then “multiply” “ not in the first parentheses

    75def logical_equality(first_input, second_input):
    76    return (
    77        (not not first_input not and not second_input)
    78        # (not (not first_input and second_input))
    79        and
    80        (not (first_input and not second_input))
    81    )
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    

    I change “not and” to “or

    75def logical_equality(first_input, second_input):
    76    return (
    77        (not not first_input or not second_input)
    78        # (not (not first_input and second_input))
    79        and
    80        (not (first_input and not second_input))
    81    )
    

    green again

  • I remove the commented line and “multiply” not in the next statement

    75def logical_equality(first_input, second_input):
    76    return (
    77        (not not first_input or not second_input)
    78        and
    79        (not first_input not and not not second_input)
    80        # (not (first_input and not second_input))
    81    )
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    
  • I change “not and” to “or

    75def logical_equality(first_input, second_input):
    76    return (
    77        (not not first_input or not second_input)
    78        and
    79        (not first_input or not not second_input)
    80        # (not (first_input and not second_input))
    81    )
    

    the test is green again

  • I remove the commented line and the “not not” from both parentheses since they cancel out

    75def logical_equality(first_input, second_input):
    76    return (
    77        (first_input or not second_input)
    78        and
    79        (not first_input or second_input)
    80    )
    

    still green

  • The 2 cases where logical_equality returns True are when first_input and second_input are the same, which means I can write a much simpler return statement thanks to the equality symbol (2 equal signs together =+=)

    75def logical_equality(first_input, second_input):
    76    return first_input == second_input
    77    return (
    78        (first_input or not second_input)
    79        and
    80        (not first_input or second_input)
    81    )
    

    the test is still green

Logical Equality returns


test_material_implication

RED: make it fail

I add a new test for one more Binary Operation in test_truth_table.py

115        self.assertTrue(src.truth_table.logical_equality(False, False))
116
117    def test_material_implication(self):
118        self.assertTrue(src.truth_table.material_implication(True, True))
119
120
121# Exceptions seen

the terminal shows AttributeError

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

GREEN: make it pass

I add a method for material_implication in truth_table.py

75def logical_equality(first_input, second_input):
76    return first_input == second_input
77    return (
78        (first_input or not second_input)
79        and
80        (not first_input or second_input)
81    )
82
83
84def material_implication(first_input, second_input):
85    return True

the test passes. material_implication returns True when the two inputs are True

REFACTOR: make it better

  • I add the next case to test_material_implication in test_truth_table.py

    117    def test_material_implication(self):
    118        self.assertTrue(src.truth_table.material_implication(True, True))
    119        self.assertFalse(src.truth_table.material_implication(True, False))
    

    the terminal shows AssertionError

    AssertionError: True is not false
    
  • I add an if statement to material_implication in truth_table.py

    84def material_implication(first_input, second_input):
    85    if first_input and not second_input:
    86        return False
    87    return True
    

    the test passes. material_implication returns

  • I add the next case to test_material_implication in test_truth_table.py

    117    def test_material_implication(self):
    118        self.assertTrue(src.truth_table.material_implication(True, True))
    119        self.assertFalse(src.truth_table.material_implication(True, False))
    120        self.assertTrue(src.truth_table.material_implication(False, True))
    

    the test is still green. material_implication returns

  • I add the fourth case

    117    def test_material_implication(self):
    118        self.assertTrue(src.truth_table.material_implication(True, True))
    119        self.assertFalse(src.truth_table.material_implication(True, False))
    120        self.assertTrue(src.truth_table.material_implication(False, True))
    121        self.assertTrue(src.truth_table.material_implication(False, False))
    122
    123# Exceptions seen
    

    the test is still passing. material_implication returns

  • there is only one case where material_implication returns False, I add a return statement for it with Logical Negation since the if statement returns False in truth_table.py

    84def material_implication(first_input, second_input):
    85    return not (first_input and not second_input)
    86    if first_input and not second_input:
    87        return False
    88    return True
    

    the test is still green

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

    84def material_implication(first_input, second_input):
    85    return (not first_input) (not and) (not not second_input)
    86    return not (first_input and not second_input)
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    
  • I change “not and” to “or

    84def material_implication(first_input, second_input):
    85    return (not first_input) or (not not second_input)
    86    return not (first_input and not second_input)
    

    the test is green again

  • I remove “not not

    84def material_implication(first_input, second_input):
    85    return not first_input or second_input
    86    return (not first_input) or (not not second_input)
    

    the test is still passing

  • I remove the other statement

    84def material_implication(first_input, second_input):
    85    return not first_input or second_input
    

Material Implication aka Logical Implication returns

it is the opposite or Logical Negation of Material NonImplication which only returns True when the first input is True and the second input is False


review

Binary Operations take 2 inputs, each input can be True or False, if we name the first input first_input and the second one second_input, the tests show that

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?

Would you like to test the truth table tests?


please leave a review