truth table

Sometimes I want programs to choose what to do based on inputs or conditions, and can make this happen with if statements. For example

  • If a person is younger than 18

    • the person cannot get a license.

    • the person cannot vote.

  • If a person is 18 or older

    • and passes a test, the person can get a license.

    • and is a citizen, the person can vote.

These exercises cover writing conditional expressions in Python with the Truth Table from Mathematics and the assertFalse and assertTrue methods.

The operations in these chapters are fundamental to how the computer works. All operations from the Truth Table always return False (which can be thought of as 0) or True (which can be thought of as 1).


start the project

  • I open a terminal

    • I open makePythonTdd.sh

    • I open makePythonTdd.ps1

  • I name this project truth_table

    • I change more_magic to truth_table in makePythonTdd.sh

       1#!/bin/bash
       2uv init truth_table
       3cd truth_table
       4mkdir tests
       5touch tests/__init__.py
       6
       7echo "import unittest
       8
       9
      10class TestTruthTable(unittest.TestCase):
      11
      12    def test_failure(self):
      13        self.assertFalse(True)
      14
      15
      16# Exceptions seen
      17# AssertionError
      18" > tests/test_truth_table.py
      19
      20echo "pytest" > requirements.txt
      21echo "pytest-watcher" >> requirements.txt
      22uv add --requirement requirements.txt
      23uv run pytest-watcher . --now
      
    • I run makePythonTdd.sh in the terminal to make the truth_table project

      ./makePythonTdd.sh
      
    • I change more_magic to truth_table in makePythonTdd.ps1

       1uv init truth_table
       2cd truth_table
       3New-Item "src/truth_table.py"
       4mkdir tests
       5New-Item tests/__init__.py
       6
       7"import unittest
       8
       9
      10class TestTruthTable(unittest.TestCase):
      11
      12    def test_failure(self):
      13        self.assertFalse(True)
      14
      15
      16# Exceptions seen
      17# AssertionError
      18" | Out-File "tests/test_truth_table.py" -Encoding UTF8
      19
      20"pytest" | Out-File requirements.txt -Encoding UTF8
      21"pytest-watcher" >> requirements.txt
      22uv add --requirement requirements.txt
      23uv run pytest-watcher . --now
      
    • I run makePythonTdd.ps1 in the terminal to make the truth_table project

      .\makePythonTdd.ps1
      

    the terminal is my friend, and shows AssertionError

    ======================== FAILURES =========================
    ______________ TestTruthTable.test_failure ________________
    
    self = <tests.test_truth_table.TestTruthTable testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertFalse(True)
    E       AssertionError: True is not false
    
    tests/test_truth_table.py:7: AssertionError
    ================ short test summary info ==================
    FAILED tests/test_truth_table.py::TestTruthTable::test_failure - AssertionError: True is not false
    ==================== 1 failed in X.YZs ====================
    
  • I hold ctrl (Windows/Linux) or option/command (MacOS) on the keyboard and use the mouse to click on tests/test_truth_table.py:7 to open it

  • I change True to False in test_truth_table.py

     4class TestTruthTable(unittest.TestCase):
     5
     6    def test_failure(self):
     7        # self.assertFalse(True)
     8        self.assertFalse(False)
     9
    10
    11# Exceptions seen
    

    the test passes.

  • I close test_truth_table.py

  • I close makePythonTdd.sh

  • I close makePythonTdd.ps1


truth table operations

Do you want to see all the CODE for the Truth Table?


what is next?

Nullary and Unary Operations