python:turtle:programmverzweigungen
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
python:turtle:programmverzweigungen [2022/04/27 11:50] – lutz | python:turtle:programmverzweigungen [2024/04/09 07:00] (aktuell) – lutz | ||
---|---|---|---|
Zeile 11: | Zeile 11: | ||
* Die **zweiseitige Alternative** kann man wie folgt formulieren: | * Die **zweiseitige Alternative** kann man wie folgt formulieren: | ||
+ | |||
+ | <code python> | ||
+ | if < | ||
+ | ... anweisungen ... | ||
+ | else: | ||
+ | ... anweisungen ... | ||
+ | </ | ||
+ | |||
+ | * Die **Mehrfachverzweigung** kann man wie folgt formulieren: | ||
+ | |||
+ | <code python> | ||
+ | if < | ||
+ | ... anweisungen ... | ||
+ | elif < | ||
+ | ... anweisungen ... | ||
+ | elif < | ||
+ | ... anweisungen ... | ||
+ | ... | ||
+ | else: | ||
+ | ... anweisungen ... | ||
+ | </ | ||
+ | |||
+ | **Bemerkungen: | ||
+ | |||
+ | * Hinter dem Doppelpunkt rückt man den Quelltext ein (Tabulator). Alle Anweisungen die (um die selbe Tiefe) eingerückt sind, werden bei der entsprechenden Bedingung ausgeführt. | ||
+ | * Zum Formulieren der Bedingungen benötigen wir Vergleichsoperatoren: | ||
+ | |||
+ | ^Operator ^Wirkung^ | ||
+ | ^ '' | ||
+ | ^ '' | ||
+ | ^ '' | ||
+ | ^ '' | ||
+ | ^ '' | ||
+ | ^ '' | ||
+ | |||
+ | Ausführlichere Informationen zum Thema findet ihr hier | ||
+ | |||
+ | [[python: | ||
+ | |||
+ | [[python: | ||
+ | |||
+ | |||
+ | Die folgenden drei Turtle-Programme sollen die drei Fälle demonstrieren: | ||
+ | |||
+ | <code python> | ||
+ | # Beispiel einseitige Alternative | ||
+ | from turtle import * | ||
+ | |||
+ | eingabe = textinput(" | ||
+ | |||
+ | # Zum Vergleich wird ein doppeltes = verwendet. | ||
+ | if eingabe == " | ||
+ | fd(200) | ||
+ | right(90) | ||
+ | fd(200) | ||
+ | right(90) | ||
+ | fd(200) | ||
+ | right(90) | ||
+ | fd(200) | ||
+ | |||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | |||
+ | <code python> | ||
+ | # Beispiel zweiseitige Alternative | ||
+ | from turtle import * | ||
+ | |||
+ | eingabe = textinput(" | ||
+ | |||
+ | # Zum Vergleich wird ein doppeltes = verwendet. | ||
+ | if eingabe == " | ||
+ | fd(200) | ||
+ | right(90) | ||
+ | fd(200) | ||
+ | right(90) | ||
+ | fd(200) | ||
+ | right(90) | ||
+ | fd(200) | ||
+ | else: | ||
+ | fd(200) | ||
+ | right(120) | ||
+ | fd(200) | ||
+ | right(120) | ||
+ | fd(200) | ||
+ | |||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | |||
+ | <code python> | ||
+ | # Beispiel Mehrfachverzweigung | ||
+ | from turtle import * | ||
+ | |||
+ | figur = textinput(" | ||
+ | stiftfarbe = textinput(" | ||
+ | fuellfarbe = textinput(" | ||
+ | |||
+ | if stiftfarbe == " | ||
+ | pencolor(" | ||
+ | elif stiftfarbe == " | ||
+ | pencolor(" | ||
+ | else: | ||
+ | pencolor(" | ||
+ | |||
+ | if fuellfarbe == " | ||
+ | fillcolor(" | ||
+ | elif fuellfarbe == " | ||
+ | fillcolor(" | ||
+ | else: | ||
+ | fillcolor(" | ||
+ | |||
+ | |||
+ | pensize(5) | ||
+ | |||
+ | if figur == " | ||
+ | begin_fill() | ||
+ | fd(200) | ||
+ | right(120) | ||
+ | fd(200) | ||
+ | right(120) | ||
+ | fd(200) | ||
+ | end_fill() | ||
+ | elif figur == " | ||
+ | begin_fill() | ||
+ | fd(200) | ||
+ | right(90) | ||
+ | fd(200) | ||
+ | right(90) | ||
+ | fd(200) | ||
+ | right(90) | ||
+ | fd(200) | ||
+ | end_fill() | ||
+ | else: | ||
+ | begin_fill() | ||
+ | fd(200) | ||
+ | right(60) | ||
+ | fd(200) | ||
+ | right(60) | ||
+ | fd(200) | ||
+ | right(60) | ||
+ | fd(200) | ||
+ | right(60) | ||
+ | fd(200) | ||
+ | right(60) | ||
+ | fd(200) | ||
+ | end_fill() | ||
+ | </ | ||
+ | |||
+ | |||
+ | Farbwähler: | ||
+ | |||
+ | **Aufgabe 12** | ||
+ | |||
+ | Teste die Beispielprogramme. Ergänze das Programm zur Mehrfachverzweigung um weitere Farben. | ||
+ | |||
+ | **Aufgabe 13** | ||
+ | |||
+ | Schreibe ein Programm bei dem zunächst folgende Eigenschaften abgefragt werden: | ||
+ | |||
+ | Figur: Dreieck, Quadrat, Sechseck | ||
+ | |||
+ | Stiftfarbe: rot, grün, blau | ||
+ | |||
+ | Füllfarbe: rot, grün, blau | ||
+ | |||
+ | entsprechend der Eingabe soll dann die entsprechende Figur gezeichnet werden. | ||
+ |
python/turtle/programmverzweigungen.1651053029.txt.gz · Zuletzt geändert: 2022/04/27 11:50 von lutz