truth table: Nullary and Unary Operations
questions about Nullary and Unary Operations
Here are questions you can answer after going through this chapter
requirements
Nullary Operations
There are 2 Nullary operations - Logical True and Logical False. They do NOT take input and return True or False
test_logical_true
RED: make it fail
I change the class and method in test_truth_table.py
1import unittest
2import src.truth_table
3
4
5class TestNullaryOperations(unittest.TestCase):
6
7 def test_logical_true(self):
8 self.assertTrue(src.truth_table.logical_true())
9
10
11# Exceptions seen
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_true'
I have not added a definition for logical_true to truth_table.py in the src folder
GREEN: make it pass
I add it to the list of Exceptions seen in
test_truth_table.py11# Exceptions seen 12# AssertionError 13# AttributeErrorI open
truth_table.pyfrom thesrcfolder in the editor of my Integrated Development Environment (IDE), then I add a function1def logical_true(): 2 return Nonethe terminal shows AssertionError
AssertionError: None is not trueI change None to True in the return statement
1def logical_true(): 2 return Truethe test passes
Adding the
logical_truefunction totruth_table.pysolved the AttributeErrorlogical_truealways returns Truelogical_truedoes not take any input
test_logical_false
RED: make it fail
I add another test in test_truth_table.py
7 def test_logical_true(self):
8 self.assertTrue(src.truth_table.logical_true())
9
10 def test_logical_false(self):
11 self.assertFalse(src.truth_table.logical_false())
12
13
14# Exceptions seen
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_false'. Did you mean: 'logical_true'?
I have not added a definition for logical_false to truth_table.py, I only added one for logical_true
GREEN: make it pass
I add a function definition in
truth_table.py1def logical_true(): 2 return True 3 4 5def logical_false(): 6 return Truethe terminal shows AssertionError
AssertionError: True is not falseI change True to False in the return statement
5def logical_false(): 6 return Falsethe test passes
Adding the
logical_trueandlogical_falsefunction totruth_table.pysolved the AttributeError in both caseslogical_truealways returns Truelogical_falsealways returns Falseboth Nullary Operations do not take input
Unary Operations
There are 2 unary operations
they each take one input and return True or False
test_logical_identity
RED: make it fail
I add a new TestCase and a test for Unary Operations to test_truth_table.py
10 def test_logical_false(self):
11 self.assertFalse(src.truth_table.logical_false())
12
13
14class TestUnaryOperations(unittest.TestCase):
15
16 def test_logical_identity(self):
17 self.assertTrue(src.truth_table.logical_identity(True))
18
19
20# Exceptions seen
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_identity'
I need to add a definition for it
GREEN: make it pass
I add the function to
truth_table.py5def logical_false(): 6 return False 7 8 9def logical_identity(): 10 return FalseTypeError: logical_identity() takes 0 positional arguments but 1 was givenI need to make the function accept input since the test sends True as input
I add the error to the list of Exceptions seen in
test_truth_table.py20# Exceptions seen 21# AssertionError 22# AttributeError 23# TypeErrorI add a name in parentheses for
logical_identityto take input intruth_table.py9def logical_identity(the_input): 10 return Falsethe terminal shows AssertionError
AssertionError: False is not trueI change the return statement
9def logical_identity(the_input): 10 return Truethe test passes
REFACTOR: make it better
I add another line to
test_logical_identityintest_truth_table.pyfor the case whenlogical_identitygets False as input16 def test_logical_identity(self): 17 self.assertTrue(src.truth_table.logical_identity(True)) 18 self.assertFalse(src.truth_table.logical_identity(False))the terminal shows AssertionError
AssertionError: True is not falseI change the return statement of
logical_identityintruth_table.py9def logical_identity(the_input): 10 return Falsethe terminal shows AssertionError
AssertionError: False is not truethere is a failure for the test that passed before. The expectation of the test is that when True is given, the result is True and when False is given, the result is False
I change the return statement of the function
9def logical_identity(the_input): 10 return the_inputthe test passes.
logical_identity returns its input as output. I think of it as a passthrough function, it does not do anything with what it gets, it only passes it along.
test_logical_negation
RED: make it fail
I add a new test to test_truth_table.py for logical_negation
16 def test_logical_identity(self):
17 self.assertTrue(src.truth_table.logical_identity(True))
18 self.assertFalse(src.truth_table.logical_identity(False))
19
20 def test_logical_negation(self):
21 self.assertFalse(src.truth_table.logical_negation(True))
22
23
24# Exceptions seen
the terminal shows AttributeError
AttributeError: module 'src.truth_table' has no attribute 'logical_negation'
GREEN: make it pass
I add a definition for the function in truth_table.py
9def logical_identity(the_input):
10 return the_input
11
12
13def logical_negation(the_input):
14 return the_input
the terminal shows AssertionError
AssertionError: True is not false
I change the return statement
13def logical_negation(the_input):
14 return False
the test passes
REFACTOR: make it better
I add another line to
test_logical_negationintest_truth_table.py20 def test_logical_negation(self): 21 self.assertFalse(src.truth_table.logical_negation(True)) 22 self.assertTrue(src.truth_table.logical_negation(False))the terminal shows AssertionError
AssertionError: False is not trueI change the return statement of the
logical_negationfunction intruth_table.py13def logical_negation(the_input): 14 return Truethe terminal shows AssertionError
AssertionError: True is not falsethe test fails for the line that passed before
I make the function return its input, remember the identity function
13def logical_negation(the_input): 14 return the_inputthe terminal shows AssertionError
AssertionError: True is not falsethe expectation of the test is that when True is given, the result is False and when False is given, the result is True
how to return the opposite of a boolean
I can make this happen with the “not” keyword which returns the opposite of the boolean after it. I add it to the return statement
13def logical_negation(the_input): 14 return not the_inputthe test passes
I change the name of the test
14class TestUnaryOperations(unittest.TestCase): 15 16 def test_logical_identity(self): 17 self.assertTrue(src.truth_table.logical_identity(True)) 18 self.assertFalse(src.truth_table.logical_identity(False)) 19 20 def test_logical_negation_aka_not(self): 21 self.assertFalse(src.truth_table.logical_negation(True)) 22 self.assertTrue(src.truth_table.logical_negation(False)) 23 24 25# Exceptions seen
logical_negation also known as not returns the opposite of its input
close the project
I close
test_truth_table.pyandtruth_table.pyin the editorI click in the terminal and use q on the keyboard to leave the tests and 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 the following tests for Nullary
and
for Unary operations
How many questions can you answer after going through this chapter?
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 binary operations?
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