truth table: test truth table tests
I want to write a program that makes the tests in tests/test_binary.py and tests/test_nullary_unary.py pass without looking at them
requirements
open the project
I make sure I am in the
pumping_pythonfolder with pwd in the terminalpwdif the terminal does not show
.../pumping_pythonI change directory to the
pumping_pythonfolderI change directory to the truth_table project
cd truth_tablethe terminal shows
.../pumping_python/truth_tableI run the tests with pytest-watcher
uv run pytest-watcher . --nowthe terminal is my friend, and shows
rootdir: .../pumping_python/truth_table configfile: pyproject.toml collected 20 items tests/test_binary.py .................... [ 80%] tests/test_nullary_unary.py .... [100%] ================== 20 passed in G.HIs ===================
RED: make it fail
I open
__init__.pyfrom thetruth_tablefolder in thesrcfolderI delete everything in
src/truth_table/__init__.py, the terminal is my friend, and shows 20 failures, I start with the last oneAttributeError: module 'src.truth_table' has no attribute 'logical_negation'because …
Can you make the tests pass without looking at how I solve it below? You can come back to compare solutions when you are done or if you get stuck.
GREEN: make it pass
I add the name to
src/truth_table/__init__.py1logical_negationthe terminal is my friend, and shows NameError
NameError: name 'logical_negation' is not definedI point it to None to define it
1logical_negation = Nonethe terminal is my friend, and shows TypeError
TypeError: 'NoneType' object is not callablebecause I cannot call None like a function.
I make logical_negation a function
1# logical_negation = None 2def logical_negation(): 3 return Nonethe terminal is my friend, and shows TypeError
TypeError: logical_negation() takes 0 positional arguments but 1 was givenI add a name in parentheses
1# logical_negation = None 2# def logical_negation(): 3def logical_negation(something): 4 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: None is not truethe assertion expects True.
I change None in the return statement to give the test what it wants
1# logical_negation = None 2# def logical_negation(): 3def logical_negation(something): 4 return True 5 return Nonethe terminal shows AssertionError
AssertionError: True is not falseI change the return statement
1# logical_negation = None 2# def logical_negation(): 3def logical_negation(something): 4 return False 5 return True 6 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: False is not trueI change the return statement to see the difference between the input and what the assertion expects, remember the identity_function?
1# logical_negation = None 2# def logical_negation(): 3def logical_negation(something): 4 return something 5 return False 6 return True 7 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: True is not falseI use not in the return statement
1# logical_negation = None 2# def logical_negation(): 3def logical_negation(something): 4 return not something 5 return something 6 return False 7 return True 8 return Nonethe terminal shows
19 failed, 1 passed, progress! It also shows AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'logical_identity'I add a function definition for logical_identity
8 return None 9 10 11def logical_identity(): 12 return Nonethe terminal is my friend, and shows TypeError
TypeError: logical_identity() takes 0 positional arguments but 1 was givenI add a name in parentheses
11# def logical_identity(): 12def logical_identity(something): 13 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: None is not trueI return what the test wants
11# def logical_identity(): 12def logical_identity(something): 13 return True 14 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: True is not falseI change the return statement
11# def logical_identity(): 12def logical_identity(something): 13 return False 14 return True 15 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: False is not truefrom what I have seen so far, this test expects
I change the return statement to see the difference between the input and what the assertion expects
11# def logical_identity(): 12def logical_identity(something): 13 return something 14 return False 15 return True 16 return Nonethe terminal shows
18 failed, 2 passed, more progress! It also shows AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'logical_true'I add logical_true
16 return None 17 18 19def logical_true(): 20 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: None is not trueI change the return statement for logical_true to give the test what it wants
19def logical_true(): 20 return True 21 return None17 failed, 3 passed. That was simple. the terminal is my friend, and shows AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'logical_false'. Did you mean: 'logical_true'?I add logical_false
21 return None 22 23 24def logical_false(): 25 return Nonethe terminal shows
16 failed, 4 passed, simple again. I am getting it. The terminal also shows AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'tautology'I add tautology
25 return None 26 27 28def tautology(): 29 return Nonethe terminal is my friend, and shows TypeError
TypeError: tautology() takes 0 positional arguments but 2 were givenI add two names in parentheses for the function to take input arguments
28# def tautology(): 29def tautology(first, second): 30 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: None is not trueI change None to True in the return statement
28# def tautology(): 29def tautology(first, second): 30 return True 31 return Nonethe terminal shows
15 failed, 5 passed, Yes! It also shows AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'project_second'I add project_second
31 return None 32 33 34def project_second(): 35 return Nonethe terminal is my friend, and shows TypeError
TypeError: project_second() takes 0 positional arguments but 2 were givenI add two names in parentheses for the function to take input arguments
34# def project_second(): 35def project_second(first, second): 36 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: None is not trueI change the return statement to give the test what it wants
34# def project_second(): 35def project_second(first, second): 36 return True 37 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: True is not falseI change the return statement
34# def project_second(): 35def project_second(first, second): 36 return False 37 return True 38 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: False is not truefrom what I have seen so far, this test expects
I change the return statement to see the difference between the input and what the assertion expects
34# def project_second(): 35def project_second(first, second): 36 return first, second 37 return False 38 return True 39 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: (True, False) is not falsesecondis False and is equal to the expectation of the assertion.I remove
firstfrom the return statement sincesecondis False34# def project_second(): 35def project_second(first, second): 36 return second 37 return first, second 38 return False 39 return True 40 return Nonethe terminal shows
14 failed, 6 passed, and AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'project_first'I add a function definition for project_first
40 return None 41 42 43def project_first(): 44 return Nonethe terminal is my friend, and shows TypeError
TypeError: project_first() takes 0 positional arguments but 2 were givenokay, I have seen this before.
I add two names in the parentheses
43# def project_first(): 44def project_first(first, second): 45 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: None is not trueI change the return statement to give the test what it wants
43# def project_first(): 44def project_first(first, second): 45 return True 46 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: True is not falseI change the return statement
43# def project_first(): 44def project_first(first, second): 45 return False 46 return True 47 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: False is not truethis test expects
I change the return statement to see the difference between the input and what the assertion expects
43# def project_first(): 44def project_first(first, second): 45 return first, second 46 return False 47 return True 48 return Nonethe terminal is my friend, and shows AssertionError
AssertionError: (False, True) is not falsefirstis False and is equal to the expectation of the assertion.I remove
secondfrom the return statement sincefirstis False43# def project_first(): 44def project_first(first, second): 45 return first 46 return first, second 47 return False 48 return True 49 return Nonethe terminal shows
13 failed, 7 passed, and AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'negate_second'I add a function definition for negate_second with a return statement to see the difference between the input and what the assertion expects
49 return None 50 51 52def negate_second(first, second): 53 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, True) is not falseI add an if statement for
(True, True)to negate_second sincefirstandsecondare both not False52def negate_second(first, second): 53 if (first, second) == (True, True): return False 54 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (False, True) is not falseI add an if statement for
(False, True)to negate_second52def negate_second(first, second): 53 if (first, second) == (True, True): return False 54 if (first, second) == (False, True): return False 55 return first, secondthe terminal is my friend, and shows
12 failed, 8 passedwith AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'negate_first'I add a function definition for negate_first with a return statement to see the difference between the input and what the assertion expects
55 return first, second 56 57 58def negate_first(first, second): 59 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, True)I add an if statement for
(True, True)to negate_first sincefirstandsecondare both not False58def negate_first(first, second): 59 if (first, second) == (True, True): return False 60 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, False) is not falseI add an if statement for
(True, False)to negate_first58def negate_first(first, second): 59 if (first, second) == (True, True): return False 60 if (first, second) == (True, False): return False 61 return first, secondthe terminal is my friend, and shows
11 failed, 9 passedwith AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'material_non_implication'I add a function definition for material_non_implication with a return statement to see the difference between the input and what the assertion expects
61 return first, second 62 63 64def material_non_implication(first, second): 65 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, True) is not falseI add an if statement for
(True, True)to material_non_implication sincefirstandsecondare both not False64def material_non_implication(first, second): 65 if (first, second) == (True, True): return False 66 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (False, True) is not falseI add an if statement for
(False, True)to material_non_implication64def material_non_implication(first, second): 65 if (first, second) == (True, True): return False 66 if (first, second) == (False, True): return False 67 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (False, False) is not falseI add an if statement for
(False, False)to material_non_implication64def material_non_implication(first, second): 65 if (first, second) == (True, True): return False 66 if (first, second) == (False, True): return False 67 if (first, second) == (False, False): return False 68 return first, secondthe terminal is my friend, and shows
10 failed, 10 passedwith AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'material_implication'I add a function definition for material_implication with a return statement to see the difference between the input and what the assertion expects
68 return first, second 69 70 71def material_implication(first, second): 72 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, False) is not falseI add an if statement for
(True, False)to material_implication71def material_implication(first, second): 72 if (first, second) == (True, False): return False 73 return first, secondthe terminal is my friend, and shows
9 failed, 11 passedwith AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'logical_nor'I add a function definition for logical_nor
73 return first, second 74 75 76def logical_nor(first, second): 77 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, True) is not falseI add an if statement for
(True, True)to logical_nor76def logical_nor(first, second): 77 if (first, second) == (True, True): return False 78 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, False) is not falseI add an if statement for
(True, False)to logical_nor76def logical_nor(first, second): 77 if (first, second) == (True, True): return False 78 if (first, second) == (True, False): return False 79 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (False, True) is not falseI add an if statement for
(False, True)to logical_nor76def logical_nor(first, second): 77 if (first, second) == (True, True): return False 78 if (first, second) == (True, False): return False 79 if (first, second) == (False, True): return False 80 return first, secondthe terminal is my friend, and shows
8 failed, 12 passedwith AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'logical_nand'. Did you mean: 'logical_nor'?I add a function definition for logical_nand
80 return first, second 81 82 83def logical_nand(first, second): 84 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, True) is not falseI add an if statement for
(True, True)to logical_nand83def logical_nand(first, second): 84 if (first, second) == (True, True): return False 85 return first, secondthe terminal is my friend, and shows
7 failed, 13 passedwith AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'logical_equality'. Did you mean: 'logical_identity'?I add logical_equality
85 return first, second 86 87 88def logical_equality(first, second): 89 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, False) is not falseI add an if statement for
(True, False)to logical_equality88def logical_equality(first, second): 89 if (first, second) == (True, False): return False 90 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (False, True) is not falseI add an if statement for
(False, True)to logical_equality88def logical_equality(first, second): 89 if (first, second) == (True, False): return False 90 if (first, second) == (False, True): return False 91 return first, secondthe terminal is my friend, and shows
6 failed, 14 passedwith AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'logical_disjunction'I add a function definition for logical_disjunction
91 return first, second 92 93 94def logical_disjunction(first, second): 95 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (False, False) is not falseI add an if statement for
(False, False)to logical_disjunction94def logical_disjunction(first, second): 95 if (first, second) == (False, False): return False 96 return first, secondthe terminal is my friend, and shows
5 failed, 15 passedwith AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'logical_conjunction'. Did you mean: 'logical_disjunction'?I add a function definition for logical_conjunction
96 return first, second 97 98 99def logical_conjunction(first, second): 100 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, False) is not falseI add an if statement for
(True, False)to logical_conjunction99def logical_conjunction(first, second): 100 if (first, second) == (True, False): return False 101 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (False, True) is not falseI add an if statement for
(False, True)to logical_conjunction99def logical_conjunction(first, second): 100 if (first, second) == (True, False): return False 101 if (first, second) == (False, True): return False 102 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (False, False) is not falseI add an if statement for
(False, False)to logical_conjunction99def logical_conjunction(first, second): 100 if (first, second) == (True, False): return False 101 if (first, second) == (False, True): return False 102 if (first, second) == (False, False): return False 103 return first, secondthe terminal is my friend, and shows
4 failed, 16 passedwith AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'exclusive_disjunction'I add a function definition for exclusive_disjunction
103 return first, second 104 105 106def exclusive_disjunction(first, second): 107 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, True) is not falseI add an if statement for
(True, True)to exclusive_disjunction106def exclusive_disjunction(first, second): 107 if (first, second) == (True, True): return False 108 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (False, False) is not falseI add an if statement for
(False, False)to exclusive_disjunction106def exclusive_disjunction(first, second): 107 if (first, second) == (True, True): return False 108 if (first, second) == (False, False): return False 109 return first, secondthe terminal is my friend, and shows
3 failed, 17 passedwith AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'converse_non_implication'. Did you mean: 'material_non_implication'?I add converse_non_implication
109 return first, second 110 111 112def converse_non_implication(first, second): 113 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, True) is not falseI add an if statement for
(True, True)to converse_non_implication112def converse_non_implication(first, second): 113 if (first, second) == (True, True): return False 114 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, False) is not falseI add an if statement for
(True, False)to converse_non_implication112def converse_non_implication(first, second): 113 if (first, second) == (True, True): return False 114 if (first, second) == (True, False): return False 115 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (False, False) is not falseI add an if statement for
(False, False)to converse_non_implication112def converse_non_implication(first, second): 113 if (first, second) == (True, True): return False 114 if (first, second) == (True, False): return False 115 if (first, second) == (False, False): return False 116 return first, secondthe terminal is my friend, and shows
2 passed, 18 failedfor AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'converse_implication'. Did you mean: 'converse_non_implication'?I add converse_implication
116 return first, second 117 118 119def converse_implication(first, second): 120 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (False, True) is not falseI add an if statement for
(False, True)to converse_implication119def converse_implication(first, second): 120 if (first, second) == (False, True): return False 121 return first, secondthe terminal is my friend, and shows
1 failed, 19 passedwith AttributeErrorAttributeError: module 'src.truth_table' has no attribute 'contradiction'I add contradiction
121 return first, second 122 123 124def contradiction(first, second): 125 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, True) is not falseI add an if statement for
(True, True)to contradiction124def contradiction(first, second): 125 if (first, second) == (True, True): return False 126 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (True, False) is not falseI add an if statement for
(True, False)to contradiction124def contradiction(first, second): 125 if (first, second) == (True, True): return False 126 if (first, second) == (True, False): return False 127 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (False, True) is not falseI add an if statement for
(False, True)to contradiction124def contradiction(first, second): 125 if (first, second) == (True, True): return False 126 if (first, second) == (True, False): return False 127 if (first, second) == (False, True): return False 128 return first, secondthe terminal is my friend, and shows AssertionError
AssertionError: (False, False) is not falseI add an if statement for
(False, False)to contradiction124def contradiction(first, second): 125 if (first, second) == (True, True): return False 126 if (first, second) == (True, False): return False 127 if (first, second) == (False, True): return False 128 if (first, second) == (False, False): return False 129 return first, secondthe terminal is my friend, and shows
================== 20 passed in G.HIs ===================
All the tests are passing! And the world is a better place than when I started.
REFACTOR: make it better
Wait, there is more… Since all the tests are passing, I can play with the functions I have to make them simpler and understand why my solutions work.
contradiction returns False in four cases. There are only four cases with two inputs. I add a return statement
124def contradiction(first, second): 125 return False 126 if (first, second) == (True, True): return False 127 if (first, second) == (True, False): return False 128 if (first, second) == (False, True): return False 129 if (first, second) == (False, False): return False 130 return first, secondthe test is still green.
I remove the commented lines from contradiction
124def contradiction(first, second): 125 return False
converse_implication returns False in only one case. I write out the if statement for that case, to make it clearer
119def converse_implication(first, second): 120 # if (first, second) == (False, True): return False 121 if first == False and second == True: 122 return False 123 return first, secondstill green.
I change the statement with not and True
119def converse_implication(first, second): 120 # if (first, second) == (False, True): return False 121 # if first == False and second == True: 122 if not first == True and second == True: 123 return False 124 return first, secondgreen.
I remove
== True119def converse_implication(first, second): 120 # if (first, second) == (False, True): return False 121 # if first == False and second == True: 122 # if not first == True and second == True: 123 if not first and second: 124 return False 125 return first, secondstill green.
I add a return statement because
if something: return Falsecan be written asreturn not (something)119def converse_implication(first, second): 120 return not (not first and second) 121 # if (first, second) == (False, True): return False 122 # if first == False and second == True: 123 if not first and second: 124 return False 125 return first, secondthe test is still green.
I “multiply not” by the things in the parentheses
119def converse_implication(first, second): 120 return (not not first) (not and) (not second) 121 return not (not first and second)the terminal is my friend, and shows SyntaxError
SyntaxError: invalid syntaxI change
not andto or119def converse_implication(first, second): 120 # return (not not first) (not and) (not second) 121 return (not not first) or (not second) 122 return not (not first and second)the test is green again.
I remove
not notbecause two nots make a right?119def converse_implication(first, second): 120 return first or not second 121 # return (not not first) (not and) (not second) 122 return (not not first) or (not second)the test is still green.
I remove the commented lines and other statements from converse_implication
119def converse_implication(first, second): 120 return first or not second 121 122 123def contradiction(first, second): 124 return False
I add an if statement for the only case that returns True in converse_non_implication
112def converse_non_implication(first, second): 113 if first == False and second == True: 114 return True 115 else: 116 return False 117 # if (first, second) == (True, True): return False 118 # if (first, second) == (True, False): return False 119 # if (first, second) == (False, False): return False 120 # return first, second 121 122 123def converse_implication(first, second):the test is still green.
I change
if first == Falseto terms of True112def converse_non_implication(first, second): 113 # if first == False and second == True: 114 if not first == True and second == True: 115 return True 116 else: 117 return Falsegreen.
I remove
== True112def converse_non_implication(first, second): 113 # if first == False and second == True: 114 # if not first == True and second == True: 115 if not first and second: 116 return True 117 else: 118 return Falsestill green.
I add a return statement because
if something: return Truecan be written asreturn somethingsincesomethingis grouped as True112def converse_non_implication(first, second): 113 return not first and second 114 # if first == False and second == True:the test is still green.
I remove the commented lines and other statements from converse_non_implication
112def converse_non_implication(first, second): 113 return not first and second 114 115 116def converse_implication(first, second): 117 return first or not second
I make the if statements in exclusive_disjunction simpler
106def exclusive_disjunction(first, second): 107 if first == True and second == True: 108 return False 109 if first == False and second == False: 110 return False 111 return True 112 if (first, second) == (True, True): return False 113 if (first, second) == (False, False): return False 114 return first, secondthe test is still green.
I change the new second if statement with not and True
106def exclusive_disjunction(first, second): 107 if first == True and second == True: 108 return False 109 # if first == False and second == False: 110 if not first == True and not second == True: 111 return False 112 return Truestill green.
I remove
== True106def exclusive_disjunction(first, second): 107 # if first == True and second == True: 108 if first and second: 109 return False 110 # if first == False and second == False: 111 # if not first == True and not second == True: 112 if not first and not second: 113 return False 114 return Truegreen.
I write everything in the second if statement with not because it happens two times in the line, I might as well make it three
106def exclusive_disjunction(first, second): 107 # if first == True and second == True: 108 if first and second: 109 return False 110 # if first == False and second == False: 111 # if not first == True and not second == True: 112 # if not first and not second: 113 if (not first) (not or) (not second): 114 return False 115 return Truethe terminal is my friend, and shows SyntaxError
SyntaxError: invalid syntaxI factor out the nots
106def exclusive_disjunction(first, second): 107 # if first == True and second == True: 108 if first and second: 109 return False 110 # if first == False and second == False: 111 # if not first == True and not second == True: 112 # if not first and not second: 113 # if (not first) (not or) (not second): 114 if not (first or second): 115 return False 116 return Truethe test is green .
I put the two if statements together as one
106def exclusive_disjunction(first, second): 107 if ( 108 (first and second) 109 or 110 (not (first or second)) 111 ): 112 return False 113 return True 114 # if first == True and second == True:the test is still green.
I add a return statement because
if something: return Falsecan be written asreturn not (something)106def exclusive_disjunction(first, second): 107 return not ( 108 (first and second) 109 or 110 (not (first or second)) 111 ) 112 if ( 113 (first and second) 114 or 115 (not (first or second)) 116 ):still green.
I “multiply” not by everything in the parentheses since it happens two times in the line
106def exclusive_disjunction(first, second): 107 return ( 108 (not (first and second)) 109 (not or) 110 (not (not (first or second))) 111 ) 112 return not ( 113 (first and second) 114 or 115 (not (first or second)) 116 )the terminal shows SyntaxError
SyntaxError: invalid syntaxI change
not orto and106def exclusive_disjunction(first, second): 107 return ( 108 (not (first and second)) 109 # (not or) 110 and 111 (not (not (first or second))) 112 )the test is green again.
I remove
not notbecause “the negation of a negation is …”106def exclusive_disjunction(first, second): 107 return ( 108 (not (first and second)) 109 # (not or) 110 and 111 # (not (not (first or second))) 112 (first or second) 113 )green.
I remove the commented lines and other statements from exclusive_disjunction
106def exclusive_disjunction(first, second): 107 return ( 108 (not (first and second)) 109 and 110 (first or second) 111 ) 112 113 114def converse_non_implication(first, second): 115 return not first and second
logical_conjunction has only one case that returns True. I add an if statement for it
99def logical_conjunction(first, second): 100 if first == True and second == True: 101 return True 102 else: 103 return False 104 if (first, second) == (True, False):the test is still green.
I remove
== True99def logical_conjunction(first, second): 100 # if first == True and second == True: 101 if first and second: 102 return True 103 else: 104 return Falsestill green.
I add a return statement because
if something: return Truecan be written asreturn somethingsincesomethingis grouped as True99def logical_conjunction(first, second): 100 return first and second 101 # if first == True and second == True:green.
I remove the commented line and other statements from logical_conjunction
99def logical_conjunction(first, second): 100 return first and second 101 102 103def exclusive_disjunction(first, second):
logical_disjunction has only one case that returns False, I break up the if statement
94def logical_disjunction(first, second): 95 if first == False and second == False: 96 return False 97 else: 98 return True 99 if (first, second) == (False, False):the test is still green.
I change the if statement with not and True
94def logical_disjunction(first, second): 95 # if first == False and second == False: 96 if not first == True and not second == True: 97 return False 98 else: 99 return Truestill green.
I remove
== True94def logical_disjunction(first, second): 95 # if first == False and second == False: 96 # if not first == True and not second == True: 97 if not first and not second: 98 return False 99 else: 100 return Truegreen.
I change the statement with not since it happens two times
94def logical_disjunction(first, second): 95 # if first == False and second == False: 96 # if not first == True and not second == True: 97 # if not first and not second: 98 if (not first) (not or) (not second): 99 return False 100 else: 101 return Truethe terminal is my friend, and shows SyntaxError
SyntaxError: invalid syntaxI factor out not
94def logical_disjunction(first, second): 95 # if first == False and second == False: 96 # if not first == True and not second == True: 97 # if not first and not second: 98 # if (not first) (not or) (not second): 99 if not (first or second): 100 return False 101 else: 102 return Truethe test is green again.
I add a return statement for the opposite of the if statement because
if something: return Falsecan be written asreturn not (something)94def logical_disjunction(first, second): 95 return not (not (first or second)) 96 # if first == False and second == False:still green.
I remove
not not94def logical_disjunction(first, second): 95 return first or second 96 return not (not (first or second))the test is still green.
I remove the commented lines and other statements from logical_disjunction
94def logical_disjunction(first, second): 95 return first or second 96 97 98def logical_conjunction(first, second):
I break up the if statements in logical_equality
88def logical_equality(first, second): 89 if first == True and second == False: 90 return False 91 if first == False and second == True: 92 return False 93 return True 94 if (first, second) == (True, False):the test is still green.
I change the if statements with not and True
88def logical_equality(first, second): 89 # if first == True and second == False: 90 if first == True and not second == True: 91 return False 92 # if first == False and second == True: 93 if not first == True and second == True: 94 return False 95 return Truestill green.
I remove
== True88def logical_equality(first, second): 89 # if first == True and second == False: 90 # if first == True and not second == True: 91 if first and not second: 92 return False 93 # if first == False and second == True: 94 # if not first == True and second == True: 95 if not first and second: 96 return False 97 return Truegreen.
I put the two if statements together as one
88def logical_equality(first, second): 89 if ( 90 (first and not second) 91 or 92 (not first and second) 93 ): 94 return False 95 return True 96 # if first == True and second == False:still green.
I add a return statement because …
88def logical_equality(first, second): 89 return not ( 90 (first and not second) 91 or 92 (not first and second) 93 ) 94 if ( 95 (first and not second) 96 or 97 (not first and second) 98 ):the test is still green.
I multiply not by all the symbols in parentheses
88def logical_equality(first, second): 89 return ( 90 (not (first and not second)) 91 (not or) 92 (not (not first and second)) 93 ) 94 return not ( 95 (first and not second) 96 or 97 (not first and second) 98 )the terminal is my friend, and shows SyntaxError
SyntaxError: invalid syntaxI change
not orto and88def logical_equality(first, second): 89 return ( 90 (not (first and not second)) 91 # (not or) 92 and 93 (not (not first and second)) 94 )the test is green again.
I multiply not by the symbols in the first part of the statement
88def logical_equality(first, second): 89 return ( 90 # (not (first and not second)) 91 ((not first) (not and) (not not second)) 92 # (not or) 93 and 94 (not (not first and second)) 95 )the terminal is my friend, and shows SyntaxError
SyntaxError: invalid syntaxI change
not andto or88def logical_equality(first, second): 89 return ( 90 # (not (first and not second)) 91 # ((not first) (not and) (not not second)) 92 ((not first) or (not not second)) 93 # (not or) 94 and 95 (not (not first and second)) 96 )the test is green again.
I remove
not not88def logical_equality(first, second): 89 return ( 90 # (not (first and not second)) 91 # ((not first) (not and) (not not second)) 92 # ((not first) or (not not second)) 93 (not first or second) 94 # (not or) 95 and 96 (not (not first and second)) 97 )still green.
I multiply not by the symbols in the second part of the statement
88def logical_equality(first, second): 89 return ( 90 # (not (first and not second)) 91 # ((not first) (not and) (not not second)) 92 # ((not first) or (not not second)) 93 (not first or second) 94 # (not or) 95 and 96 # (not (not first and second)) 97 ((not not first) (not and) (not second)) 98 )the terminal is my friend, and shows SyntaxError
SyntaxError: invalid syntaxI change
not andto or88def logical_equality(first, second): 89 return ( 90 # (not (first and not second)) 91 # ((not first) (not and) (not not second)) 92 # ((not first) or (not not second)) 93 (not first or second) 94 # (not or) 95 and 96 # (not (not first and second)) 97 # ((not not first) (not and) (not second)) 98 ((not not first) or (not second)) 99 )the test is green again.
I remove
not not88def logical_equality(first, second): 89 return ( 90 # (not (first and not second)) 91 # ((not first) (not and) (not not second)) 92 # ((not first) or (not not second)) 93 (not first or second) 94 # (not or) 95 and 96 # (not (not first and second)) 97 # ((not not first) (not and) (not second)) 98 # ((not not first) or (not second)) 99 (first or not second) 100 )the test is still green.
I remove the commented lines and other statements from logical_equality
88def logical_equality(first, second): 89 return ( 90 (not first or second) 91 and 92 (first or not second) 93 ) 94 95 96def logical_disjunction(first, second):
I make the if statement in logical_nand simpler
83def logical_nand(first, second): 84 if first == True and second == True: 85 return False 86 else: 87 return True 88 if (first, second) == (True, True): return False 89 return first, secondthe test is still green.
I remove
== True83def logical_nand(first, second): 84 # if first == True and second == True: 85 if first and second: 86 return False 87 else: 88 return True 89 if (first, second) == (True, True):still green.
I add a return statement because …
83def logical_nand(first, second): 84 return not (first and second) 85 # if first == True and second == True:green.
I remove the commented line and other statements from logical_nand
83def logical_nand(first, second): 84 return not (first and second) 85 86 87def logical_equality(first, second):
logical_nor has only one case that returns True. I add an if statement for it
76def logical_nor(first, second): 77 if first == False and second == False: 78 return True 79 else: 80 return False 81 if (first, second) == (True, True):the test is still green.
I use not to write the if statement in terms of True
76def logical_nor(first, second): 77 # if first == False and second == False: 78 if not first == True and not second == True: 79 return True 80 else: 81 return Falsestill green.
I remove
== True76def logical_nor(first, second): 77 # if first == False and second == False: 78 # if not first == True and not second == True: 79 if not first and not second: 80 return True 81 else: 82 return Falsegreen.
I add a return statement
76def logical_nor(first, second): 77 return not first and not second 78 # if first == False and second == False:still green.
I write the statement with not because it happens two times
76def logical_nor(first, second): 77 return (not first) (not or) (not second) 78 return not first and not secondthe terminal is my friend, and shows SyntaxError
SyntaxError: invalid syntaxI factor out not
76def logical_nor(first, second): 77 return not (first or second) 78 return (not first) (not or) (not second)the test is green again.
I remove the commented lines and other statements from logical_nor
76def logical_nor(first, second): 77 return not (first or second) 78 79 80def logical_nand(first, second):
I add a return statement for the if statement of material_implication because
if something: return Falsecan be written asreturn not (something)71def material_implication(first, second): 72 return not ((first, second) == (True, False)) 73 if (first, second) == (True, False): return False 74 return first, secondthe test is still green.
I break up the return statement to make it simpler
71def material_implication(first, second): 72 return not (first == True and second == False) 73 return not ((first, second) == (True, False))still green.
I use not to rewrite it in terms of True
71def material_implication(first, second): 72 return not (first == True and not second == True) 73 return not (first == True and second == False)green.
I remove
== True71def material_implication(first, second): 72 return not (first and not second) 73 return not (first == True and not second == True)still green.
I multiply not by everything in parentheses
71def material_implication(first, second): 72 return (not first) (not and) (not not second) 73 return not (first and not second)the terminal is my friend, and shows SyntaxError
SyntaxError: invalid syntaxI change
not andto or71def material_implication(first, second): 72 return (not first) or (not not second) 73 # return (not first) (not and) (not not second) 74 return not (first and not second)the test is green again.
I remove
not not71def material_implication(first, second): 72 return not first or second 73 return (not first) or (not not second)the test is still green.
I remove the commented lines from material_implication
71def material_implication(first, second): 72 return not first or second 73 74 75def logical_nor(first, second):
material_non_implication has 3 cases that return False. I add a return statement for the case that returns True
64def material_non_implication(first, second): 65 return (first, second) == (True, False) 66 if (first, second) == (True, True): return Falsethe test is still green.
I make the return statement simpler
64def material_non_implication(first, second): 65 return first == True and second == False 66 return (first, second) == (True, False)still green.
I use not to write it in terms of True
64def material_non_implication(first, second): 65 return first == True and not second == True 66 return first == True and second == Falsegreen.
I remove
== True64def material_non_implication(first, second): 65 return first and not second 66 return first == True and not second == Truestill green.
I remove the other statements from material_non_implication
64def material_non_implication(first, second): 65 return first and not second 66 67 68def material_implication(first, second):
firstis True in the two cases where negate_first returns False, I add an if statement for them58def negate_first(first, second): 59 if first == True: 60 return False 61 else: 62 return True 63 # if (first, second) == (True, True): return False 64 # if (first, second) == (True, False): return False 65 # return first, secondthe test is still green.
I add a return statement because …
58def negate_first(first, second): 59 return not first == True 60 if first == True:still green.
I remove
== True58def negate_first(first, second): 59 return not first 60 return not first == Truegreen.
I remove the commented lines and other statements from negate_first
58def negate_first(first, second): 59 return not first 60 61 62def material_non_implication(first, second):
secondis True in the two cases where negate_second returns False. I add an if statement for them52def negate_second(first, second): 53 if second == True: 54 return False 55 else: 56 return True 57 # if (first, second) == (True, True): return False 58 # if (first, second) == (False, True): return False 59 # return first, secondthe test is still green.
I add a return statement because
if something == False:can be written asreturn not something52def negate_second(first, second): 53 return not second == True 54 if second == True:still green.
I remove
== True52def negate_second(first, second): 53 return not second 54 return not second == Truegreen.
I remove the commented lines and other statements from negate_second
52def negate_second(first, second): 53 return not second 54 55 56def negate_first(first, second):I remove the commented line and other statements from project_first
34# def project_second(): 35def project_second(first, second): 36 return second 37 return first, second 38 return False 39 return True 40 return None 41 42 43def project_first(first, second): 44 return first 45 46 47def negate_second(first, second):I remove the commented line and other statements from project_second
28# def tautology(): 29def tautology(first, second): 30 return True 31 return None 32 33 34def project_second(first, second): 35 return second 36 37 38def project_first(first, second):I remove the commented line and second return statement from tautology
24def logical_false(): 25 return None 26 27 28def tautology(first, second): 29 return True 30 31 32def project_second(first, second):I change the return statement of logical_false since None is grouped as False
24def logical_false(): 25 return False 26 return Nonethe test is still green.
I remove the second return statement from logical_false and logical_true
11# def logical_identity(): 12def logical_identity(something): 13 return something 14 return False 15 return True 16 return None 17 18 19def logical_true(): 20 return True 21 22 23def logical_false(): 24 return False 25 26 27def tautology(first, second):I remove the commented lines and other statements from logical_identity and logical_negation
1def logical_negation(something): 2 return not something 3 4 5def logical_identity(something): 6 return something 7 8 9def logical_true():I add a git commit message in the other terminal
git commit -am 'test truth_table tests'
close the project
I close
src/truth_table/__init__.pyI click in the terminal where the tests are running
I use q on the keyboard to leave the tests. The terminal goes back to the command line.
I change directory to the parent of
truth_tablecd ..the terminal shows
.../pumping_pythonI am back in the
pumping_pythondirectory.
review
I ran tests using booleans which can be False or True, for the operations of the Truth Table from Mathematics
there are two nullary operations, they do not take input and always return the same thing, they are constant
Logical False always returns False
Logical True always returns True
there are two unary operations, they take one input
Logical Identity returns its input as output
Logical Negation returns the negation of its input as output
there are 16 binary operations, they each take 2 inputs, in this case I named the second input
secondand the first onefirst-
always returns False
never returns True
is the Logical Negation (NOT) of Tautology which always returns True
-
returns
first or not secondis the Logical Negation (NOT) of Converse Non-Implication which returns True only if
firstis False andsecondis True
-
returns
not first and secondis the Logical Negation (NOT) of Converse Implication which returns False if
firstis False andsecondis True
-
returns
(not (first and second)) and (first or second)returns True only if
firstandsecondare NOT equalis the Logical Negation (NOT) of Logical Equality which returns True only if
firstandsecondare equal
-
returns
first and secondis the Logical Negation (NOT) of Logical NAND which returns False only if
firstis True andsecondis True
-
returns
first or secondis the Logical Negation (NOT) of Logical NOR which returns True only if
firstis False andsecondis False
-
returns
(not first or second) and (first or not second)returns True only if
firstandsecondare equalis the Logical Negation (NOT) of Exclusive Disjunction (Exclusive OR) which returns True only if
firstandsecondare NOT equal
-
returns
not (first and second)is the Logical Negation (NOT) of Logical Conjunction (AND) which returns True only if
firstis True andsecondis True
-
returns
not (first or second)is the Logical Negation (NOT) of Logical Disjunction which returns False only if
firstis False andsecondis False
-
returns
not first or secondis the Logical Negation (NOT) of Material Non-Implication which returns True only if
firstis True andsecondis False
-
returns
first and not secondis the Logical Negation (NOT) of Material/Logical Implication which returns False only if
firstis True andsecondis False
-
always returns
not firstreturns True only if
firstis Falseis the Logical Negation (NOT) of Project First which returns True only if
firstis True
-
always returns
not secondreturns True only if
secondis Falseis the Logical Negation (NOT) of Project Second which returns True only if
secondis True
-
always returns
firstreturns True only if
firstis Trueis the Logical Negation (NOT) of Negate First which returns True only if
firstis False
-
always returns
secondreturns True only if
secondis Trueis the Logical Negation (NOT) of Negate Second which returns True only if
secondis False
-
always returns True
never returns False
is the Logical Negation (NOT) of contradiction which always returns False
-
and
All the binary operations or conditions have been written with some or all of the above 3.
code from the chapter
Do you want to see all the CODE I typed for the Truth Table?
what is next?
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.