Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| lehrkraefte:sbtsnr:python:logic [2022/09/30 16:15] – Karlheinz Schubert | lehrkraefte:sbtsnr:python:logic [2022/11/04 17:21] (current) – [... und nun noch etwas rumgesponnen ...] Olaf Schnürer | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Entwicklung eines Programms zur Darstellung von logischen Operationen --> Wahrheitstabellen ====== | ||
| + | |||
| + | |||
| + | <code python wahrheitstabelle.py> | ||
| + | """ | ||
| + | Print of Logic tables | ||
| + | """ | ||
| + | import operator | ||
| + | |||
| + | anzahlVariablen = 2 | ||
| + | x1 = False | ||
| + | x2 = False | ||
| + | |||
| + | for index in range(2**anzahlVariablen): | ||
| + | print( | ||
| + | f" | ||
| + | ) | ||
| + | x1 = not x1 | ||
| + | if (index + 1) % 2 == 0: | ||
| + | x2 = not x2 | ||
| + | |||
| + | |||
| + | </ | ||
| + | <hidden Show Code> | ||
| + | <code python wahrheitstabelle-step1.py> | ||
| + | """ | ||
| + | Print of Logic tables | ||
| + | """ | ||
| + | import operator | ||
| + | |||
| + | anzahlVariablen = 2 | ||
| + | x1 = False | ||
| + | x2 = False | ||
| + | |||
| + | Ueberschrift = " Index | x2 x1 | and | ||
| + | print(Ueberschrift) | ||
| + | print(" | ||
| + | for index in range(2**anzahlVariablen): | ||
| + | print( | ||
| + | f" | ||
| + | ) | ||
| + | x1 = not x1 | ||
| + | if (index + 1) % 2 == 0: | ||
| + | x2 = not x2 | ||
| + | |||
| + | </ | ||
| + | </ | ||
| + | <code python wahrheitstabelle-step2a.py> | ||
| + | """ | ||
| + | Print of Logic tables | ||
| + | """ | ||
| + | import operator | ||
| + | |||
| + | anzahlVariablen = 3 | ||
| + | x1 = False | ||
| + | x2 = False | ||
| + | x3 = False | ||
| + | |||
| + | Ueberschrift = " Index | | ||
| + | print(Ueberschrift) | ||
| + | print(" | ||
| + | for index in range(2**anzahlVariablen): | ||
| + | print( | ||
| + | f" | ||
| + | ) | ||
| + | x1 = not x1 | ||
| + | if (index + 1) % 2 == 0: | ||
| + | x2 = not x2 | ||
| + | if (index + 1) % 4 == 0: | ||
| + | x3 = not x3 | ||
| + | |||
| + | |||
| + | </ | ||
| + | <code python wahrheitstabelle-step2b.py> | ||
| + | """ | ||
| + | Print of Logic tables | ||
| + | """ | ||
| + | import operator | ||
| + | |||
| + | anzahlVariablen = 3 | ||
| + | x1 = False | ||
| + | x2 = False | ||
| + | x3 = False | ||
| + | |||
| + | Ueberschrift = " | ||
| + | print(Ueberschrift) | ||
| + | print(" | ||
| + | for index in range(2**anzahlVariablen): | ||
| + | print( | ||
| + | f" | ||
| + | ) | ||
| + | x1 = not x1 | ||
| + | if (index + 1) % 2 == 0: | ||
| + | x2 = not x2 | ||
| + | if (index + 1) % 4 == 0: | ||
| + | x3 = not x3 | ||
| + | |||
| + | |||
| + | </ | ||
| + | <code python wahrheitstabelle-step3.py> | ||
| + | """ | ||
| + | Print of Logic tables | ||
| + | """ | ||
| + | import operator | ||
| + | |||
| + | anzahlVariablen = int(input(" | ||
| + | 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, | ||
| + | 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" | ||
| + | Ueberschrift = f" | ||
| + | print(Ueberschrift) | ||
| + | print(" | ||
| + | |||
| + | for Zeile in range(2**anzahlVariablen): | ||
| + | # xTeil = [x[xIndex] for xIndex in range(len(x))] | ||
| + | xTeil = "" | ||
| + | for index in range(anzahlVariablen, | ||
| + | xTeil += f" | ||
| + | print(f" | ||
| + | Erhoehen(Zeile, | ||
| + | </ | ||
| + | |||
| + | ====== Funktion Erhoehen oben korrigiert ====== | ||
| + | |||
| + | <code python wahrheitstabelle-step3-verbessert.py> | ||
| + | """ | ||
| + | Truth tables | ||
| + | """ | ||
| + | import operator | ||
| + | |||
| + | anzahlVariablen = int(input(" | ||
| + | x = anzahlVariablen * [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 Erhoehe(nr, feld): | ||
| + | index = anzahlVariablen - 1 | ||
| + | while feld[index] == True: | ||
| + | x[index] = False | ||
| + | index -= 1 | ||
| + | if index >= 0: | ||
| + | x[index] = True | ||
| + | |||
| + | UeberschriftxTeil = "" | ||
| + | for index in range(anzahlVariablen): | ||
| + | UeberschriftxTeil += f" | ||
| + | Ueberschrift = f" | ||
| + | print(Ueberschrift) | ||
| + | print(" | ||
| + | |||
| + | for Zeile in range(2**anzahlVariablen): | ||
| + | xTeil = "" | ||
| + | for index in range(anzahlVariablen): | ||
| + | xTeil += f" | ||
| + | print(f" | ||
| + | Erhoehe(Zeile, | ||
| + | </ | ||
| + | |||
| + | ====== ... und nun noch etwas rumgesponnen ... ====== | ||
| + | |||
| + | <code python wahrheitstabelle-step3-per-binaerdarstellung.py> | ||
| + | """ | ||
| + | Truth tables | ||
| + | """ | ||
| + | leerzeichen = 4 # sollte mindestens 4 sein | ||
| + | |||
| + | # Unicode-Nummern gefunden auf https:// | ||
| + | vertikal = ' | ||
| + | horizontal = ' | ||
| + | kreuzung = ' | ||
| + | |||
| + | anzahlVariablen = int(input(" | ||
| + | |||
| + | UeberschriftVariablenTeil = "" | ||
| + | UeberschriftLogikTeil = f" | ||
| + | print(UeberschriftVariablenTeil + " " + vertikal + UeberschriftLogikTeil) | ||
| + | print(horizontal * (len(UeberschriftVariablenTeil) + 1) + kreuzung + horizontal * len(UeberschriftLogikTeil)) | ||
| + | |||
| + | for index in range(2 ** anzahlVariablen): | ||
| + | binaer = f' | ||
| + | binearMitAbstand = "" | ||
| + | # # ausführlich: | ||
| + | # und = " | ||
| + | # oder = " | ||
| + | # xOder = " | ||
| + | # print(binearMitAbstand + " vertikal" | ||
| + | |||
| + | # kurz: | ||
| + | print(binearMitAbstand + " " + vertikal + f" | ||
| + | </ | ||
| + | |||
| + | |||
| + | ====== Turtle-Test ====== | ||
| + | |||
| + | <code python> | ||
| + | from turtle import * | ||
| + | speed(0) | ||
| + | shape(" | ||
| + | |||
| + | n = 11 | ||
| + | s = 230 | ||
| + | |||
| + | pencolor(" | ||
| + | pensize(4) | ||
| + | fillcolor(" | ||
| + | |||
| + | a = 300 | ||
| + | penup() | ||
| + | goto(0.7*a, 0.5*a) | ||
| + | pendown() | ||
| + | setheading(90) | ||
| + | pendown() | ||
| + | |||
| + | begin_fill() | ||
| + | for i in range(n): | ||
| + | forward(s) | ||
| + | left(180 - 180/n) | ||
| + | end_fill() | ||
| + | |||
| + | </ | ||