lehrkraefte:sbtsnr:python:logic

This is an old revision of the document!


Entwicklung eines Programms zur Darstellung von logischen Operationen --> Wahrheitstabellen

wahrheitstabelle.py
"""
Print of Logic tables
"""
import operator
 
anzahlVariablen = 2
x1 = False
x2 = False
 
for index in range(2**anzahlVariablen):
    print(
        f"{index}  | {x2} {x1}   |   {x2 and x1}    {x2 or x1}     {x2 ^ x1}"
    )
    x1 = not x1
    if (index + 1) % 2 == 0:
        x2 = not x2

Click to display ⇲

Click to hide ⇱

wahrheitstabelle-step1.py
"""
Print of Logic tables
"""
import operator
 
anzahlVariablen = 2
x1 = False
x2 = False
 
Ueberschrift = " Index |    x2    x1  |  and   or   xor"
print(Ueberschrift)
print("-" * len(Ueberschrift))
for index in range(2**anzahlVariablen):
    print(
        f"{index}  |{x2} {x1}   |   {x2 and x1}    {int(x2 or x1)}     {int(x2 ^ x1)}"
    )
    x1 = not x1
    if (index + 1) % 2 == 0:
        x2 = not x2
wahrheitstabelle-step2a.py
"""
Print of Logic tables
"""
import operator
 
anzahlVariablen = 3
x1 = False
x2 = False
x3 = False
 
Ueberschrift = " Index |   x3     x2     x1  |  and   or   xor"
print(Ueberschrift)
print("-" * len(Ueberschrift))
for index in range(2**anzahlVariablen):
    print(
        f"{index:5}  |{x3:5}  {x2:5} {x1:5}   |   {int(x3 and x2 and x1)}    {int(x3 or x2 or x1)}     {int(x3 ^ x2 ^ x1)}"
    )
    x1 = not x1
    if (index + 1) % 2 == 0:
        x2 = not x2
    if (index + 1) % 4 == 0:
        x3 = not x3
wahrheitstabelle-step2b.py
"""
Print of Logic tables
"""
import operator
 
anzahlVariablen = 3
x1 = False
x2 = False
x3 = False
 
Ueberschrift = "Index  |   x3     x2    x1    |   and       or        xor "
print(Ueberschrift)
print("-" * len(Ueberschrift))
for index in range(2**anzahlVariablen):
    print(
        f"{index:5}  | {str(x3):5}  {str(x2):5} {str(x1):5}   |   {str(x3 and x2 and x1):5}    {str(x3 or x2 or x1):5}     {x3 ^ x2 ^ x1}"
    )
    x1 = not x1
    if (index + 1) % 2 == 0:
        x2 = not x2
    if (index + 1) % 4 == 0:
        x3 = not x3
wahrheitstabelle-step3.py
"""
Print of Logic tables
"""
import operator
 
anzahlVariablen = int(input("Anzahl Variablen [1...9]: "))
x = []
for index in range(anzahlVariablen):
    x.append(False)
 
 
def Oder():
    Wert = False
    for index in range(anzahlVariablen):
        Wert = Wert or x[index]
    return Wert
 
 
def Und():
    Wert = True
    for index in range(anzahlVariablen):
        Wert = Wert and x[index]
    return Wert
 
 
def ExOder():
    Wert = False
    for index in range(anzahlVariablen):
        Wert = Wert ^ x[index]
    return Wert
 
 
def Erhoehen(nr, feld):
    for index in range(1, anzahlVariablen + 1):
        if (nr + 1) % index == 0:
            x[index - 1] = not x[index - 1]
 
 
UeberschriftxTeil = ""
for index in range(anzahlVariablen):
    UeberschriftxTeil += f"x{index+1}  "
Ueberschrift = f"    Index    |   {UeberschriftxTeil} |  and   or   xor"
print(Ueberschrift)
print("-" * len(Ueberschrift))
 
for Zeile in range(2**anzahlVariablen):
    # xTeil = [x[xIndex] for xIndex in range(len(x))]
    xTeil = ""
    for index in range(anzahlVariablen, 0, -1):
        xTeil += f"{x[index-1]:2}  "
    print(f"{Zeile:5}  ({Zeile:02X})  |  {xTeil}  | {Und():3}  {Oder():3}  {ExOder():3}")
    Erhoehen(Zeile, x)
  • lehrkraefte/sbtsnr/python/logic.1664554536.txt.gz
  • Last modified: 2022/09/30 18:15
  • by Karlheinz Schubert