Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
| Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
| gf_informatik:programmieren_iv_gra [2025-06-26 19:14] – [Programmieren Teil 4 – Funktionen (Unterprogramme)] gra | gf_informatik:programmieren_iv_gra [2026-05-13 08:56] (aktuell) – [4. Lösungen zu den Aufträgen] gra | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | ====== Programmieren Teil 4 – Funktionen | + | ====== Programmieren Teil 4 – Funktionen ====== |
| - | * Dieses Seite ist die Fortsetzung von [[gf_informatik: | + | * Dieses Seite ist die Fortsetzung von [[gf_informatik: |
| + | * Weiter zu [[gf_informatik: | ||
| - | <nodisp 2> | ||
| - | ++++Verbessern (hidden):| | ||
| - | * Fancy Aufgaben nach O verschieben, | ||
| - | * Insbesondere mehr Aufgaben mit Listen und | ||
| - | * Verknüpfungen von Bedingungen mit logischen Operatoren | ||
| - | * Evtl. Lernziele bezüglich Verknüpfungen von Bedingungen präzisieren. | ||
| - | ++++ | ||
| - | </ | ||
| ++++Lernziele: | ++++Lernziele: | ||
| Zeile 197: | Zeile 190: | ||
| ===== - Aufgaben N – Funktionen ===== | ===== - Aufgaben N – Funktionen ===== | ||
| In folgenden Aufgaben sollst du Funktionen erstellen (= definieren) und verwenden (=aufrufen). | In folgenden Aufgaben sollst du Funktionen erstellen (= definieren) und verwenden (=aufrufen). | ||
| + | * [[.: | ||
| + | * [[.: | ||
| + | * [[.: | ||
| + | * [[.: | ||
| - | ==== Aufgaben NA – Einfache Funktionen (Unterprogramme) ohne Rückgabewerte ==== | ||
| - | Die folgenden Aufgaben behandeln Funktionen mit und ohne Argumente – aber ohne Rückgabewerte. | ||
| - | === NA1 – Blumen | + | ===== - Lösungen |
| - | Schreibe eine Funktion namens '' | + | |
| - | + | ||
| - | {{: | + | |
| - | + | ||
| - | Hinweis: Eine Blume wie im Bild kann aus sechs 30-px-Punkten ('' | + | |
| - | + | ||
| - | === NA2 – Bunte Blumen === | + | |
| - | Ändere die Funktion '' | + | |
| - | + | ||
| - | {{: | + | |
| - | + | ||
| - | === NA3 – Blumenreihen === | + | |
| - | == Schritt 1 == | + | |
| - | Ändere die Funktion '' | + | |
| - | <code python> | + | |
| - | x = 0 | + | |
| - | while x < 600: | + | |
| - | blume(" | + | |
| - | x = x + 100 | + | |
| - | </ | + | |
| - | + | ||
| - | {{: | + | |
| - | + | ||
| - | == Schritt 2 == | + | |
| - | Lösche den vorhin ergänzten Code (die vier Zeilen mit der while-Schleife) und füge stattdessen folgende Codezeilen unterhalb deiner Funktion '' | + | |
| - | <code python> | + | |
| - | colors = [" | + | |
| - | xpositions = [0, | + | |
| - | ypositions = [0, | + | |
| - | </ | + | |
| - | + | ||
| - | Schreibe einen möglichst kurzen Code mit while-Schleife, | + | |
| - | + | ||
| - | {{: | + | |
| - | + | ||
| - | === NA4 – Wer hat die schönste Blumenwiese? | + | |
| - | + | ||
| - | <WRAP group> | + | |
| - | <WRAP column twothirds> | + | |
| - | Nutze deine Funktion '' | + | |
| - | * Die Hintergrundfarbe kannst du mit der Turtle-Funktion '' | + | |
| - | * Die Farbcodes finden sich [[https:// | + | |
| - | * Erstelle eine Liste mit verschiedenen Farbnamen. Daraus wählst du später für jede Blume ein zufälliges Element. | + | |
| - | * Wähle die x-Positionen im Bereich -250 bis 250, und die y-Positionen im Bereich -100 bis 100, sodass ein Bild im Breitformat entsteht. | + | |
| - | * Mit der Funktion '' | + | |
| - | + | ||
| - | </ | + | |
| - | + | ||
| - | <WRAP column third> | + | |
| - | {{: | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | <color # | + | |
| - | + | ||
| - | + | ||
| - | ==== Aufgaben NB – Funktionen (Unterprogramme) mit Rückgabewerten ==== | + | |
| - | Die folgenden Aufgaben behandeln Funktionen mit Argumenten und Rückgabewerten. | + | |
| - | === NB1 – Franken in Kronen === | + | |
| - | Schreibe eine Funktion '' | + | |
| - | Teste deine Funktion. | + | |
| - | + | ||
| - | === NB2 – Maximalwert aus zwei Zahlen === | + | |
| - | Schreibe eine Funktion '' | + | |
| - | + | ||
| - | === NB3 – Pfannkuchen === | + | |
| - | <WRAP group> | + | |
| - | <WRAP column twothirds> | + | |
| - | Schreibe eine Funktion '' | + | |
| - | Hier die Mengen für eine Person: | + | |
| - | * 50 g Mehl | + | |
| - | * 0.125 Teelöffel Salz | + | |
| - | * 1 dl Milch | + | |
| - | * 1 Ei | + | |
| - | **Hinweis: | + | |
| - | + | ||
| - | Teste deine Funktion mit folgendem Code: | + | |
| - | + | ||
| - | </ | + | |
| - | <WRAP column third> | + | |
| - | {{: | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | <code python> | + | |
| - | n = input(" | + | |
| - | mehl, salz, milch, ei = zutaten_pfannkuchen(n) | + | |
| - | print(f" | + | |
| - | </ | + | |
| - | + | ||
| - | === NB4 – Minimalwert aus drei Zahlen === | + | |
| - | + | ||
| - | <box 100% round green|**Logische Verknüpfungen**> | + | |
| - | + | ||
| - | Mit den Schlüsselworten '' | + | |
| - | + | ||
| - | <code python> | + | |
| - | a = input(" | + | |
| - | b = input(" | + | |
| - | + | ||
| - | if a > 0 and b > 0: | + | |
| - | print(" | + | |
| - | + | ||
| - | if a > 0 or b > 0: | + | |
| - | print(" | + | |
| - | + | ||
| - | if not(a > 0 or b > 0): | + | |
| - | print(" | + | |
| - | </ | + | |
| - | + | ||
| - | * Erste Verzweigung: | + | |
| - | * Zweite Verzweigung: | + | |
| - | * Dritte Verzweigung: | + | |
| - | + | ||
| - | [[https:// | + | |
| - | + | ||
| - | </ | + | |
| - | + | ||
| - | Schreibe eine Funktion '' | + | |
| - | + | ||
| - | <code python> | + | |
| - | print(min_von_drei(1, | + | |
| - | print(min_von_drei(5, | + | |
| - | print(min_von_drei(9, | + | |
| - | </ | + | |
| - | + | ||
| - | === NB5 – Maximalwert aus Liste === | + | |
| - | Schreibe eine Funktion '' | + | |
| - | + | ||
| - | <code python> | + | |
| - | list1 = [11, 16, 5, 12, 3, 13] | + | |
| - | list2 = [13, 2, 5, 10, 1, 17, 5, 25] | + | |
| - | list3 = [-25, -13, -3, -33] | + | |
| - | </ | + | |
| - | + | ||
| - | === NB6 – Gerade oder ungerade === | + | |
| - | Schreibe eine Funktion '' | + | |
| - | + | ||
| - | <code python> | + | |
| - | zahl = input(" | + | |
| - | + | ||
| - | if ist_gerade(zahl): | + | |
| - | print(" | + | |
| - | else: | + | |
| - | print(" | + | |
| - | </ | + | |
| - | + | ||
| - | **Frage:** Bisher hatten wir in einer if-Verzeigung immer eine Bedingung mit Vergleichsoperatoren stehen. Zum Beispiel //x > 4// oder //x % 2 == 0// etc. In diesem Code steht bloss die Funktion ist_gerade(). Wie erklärst du, dass hier keine Bedingung nötig ist? Bespreche deine Antwort mit der Lehrperson. | + | |
| - | + | ||
| - | === NB7 – Pizzabestellung === | + | |
| - | + | ||
| - | <WRAP group> | + | |
| - | <WRAP column twothirds> | + | |
| - | + | ||
| - | Schreibe eine Funktion '' | + | |
| - | + | ||
| - | </ | + | |
| - | <WRAP column third> | + | |
| - | {{: | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | **Frage:** Wenn ihr zu viert seid: Bestellt ihr besser 4 oder 5 Pizzen? | + | |
| - | + | ||
| - | === NB8 – Nächte im Hotel === | + | |
| - | + | ||
| - | <WRAP group> | + | |
| - | <WRAP column twothirds> | + | |
| - | Schreibe eine Funktion '' | + | |
| - | Preise **pro Nacht**: | + | |
| - | ^ Klasse | + | |
| - | | Standard | + | |
| - | | Ausblick | + | |
| - | | Balkon | + | |
| - | + | ||
| - | **Hinweise: | + | |
| - | * Deine Funktion muss zuerst die Anzahl Einzel- und Doppelzimmer berechnen (ausgehend von der Anzahl Personen): Es ist maximal ein Einzelzimmer nötig. | + | |
| - | * Falls für die Klasse etwas anderes als " | + | |
| - | + | ||
| - | </ | + | |
| - | <WRAP column third> | + | |
| - | {{: | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | Teste deine Funktion mit folgendem Code: | + | |
| - | <code python> | + | |
| - | # Reise 1: 2 Personen, 2 Nächte, Standard: Preis = 320 | + | |
| - | print(hotel_preise(2, | + | |
| - | # Reise 2: 5 Personen, 3 Nächte, Ausblick: Preis = 1500 | + | |
| - | print(hotel_preise(5, | + | |
| - | # Reise 3: 6 Personen, 4 Nächte, Balkon: Preis = 3120 | + | |
| - | print(hotel_preise(6, | + | |
| - | # Reise 4: 1 Person, 1 Nacht, falsche Angabe: Preis = 200 | + | |
| - | print(hotel_preise(1, | + | |
| - | </ | + | |
| - | + | ||
| - | + | ||
| - | ==== Aufgaben NC – Gemischte Aufgaben zu Funktionen ==== | + | |
| - | Die folgenden Aufgaben behandeln Funktionen mit und ohne Argumente oder Rückgabewerte. | + | |
| - | + | ||
| - | + | ||
| - | === NC1 – Hexagons are the Bestagons === | + | |
| - | - Schreibe eine Funktion '' | + | |
| - | - Verwende anschliessend | + | |
| - | + | ||
| - | {{: | + | |
| - | + | ||
| - | Wenn du das geschafft hast, darfst du folgendes Video schauen: [[https:// | + | |
| - | + | ||
| - | === NC2 – Random Dots === | + | |
| - | {{: | + | |
| - | - Schreibe eine Funktion '' | + | |
| - | - Die Argumente '' | + | |
| - | - Das Argument '' | + | |
| - | - Die Funktion zeichnet eine zufällige Anzahl Punkte (20 px) im gewünschten Bereich. | + | |
| - | - Für jedem Punkt wird eine zufällige Farbe aus der Farbliste gewählt. | + | |
| - | - Die Position jedes Punktes wird ebenfalls zufällig bestimmt: Die x-Position im Bereich -250 bis 250, die y-Position im Bereich -100 bis 100. | + | |
| - | - **Die Funktion gibt zurück, wie viele Punkte sie gezeichnet hat.** | + | |
| - | - Schreibe ein Programm, das die Funktion '' | + | |
| - | - Erstelle eine Liste mit vier bis sieben Farben. | + | |
| - | - Rufe die Funktion auf (mit beliebigen Werten für min und max sowie mit der Farbliste) und gib eine Meldung in folgender Form aus: "Es wurden 14 Punkte gezeichnet" | + | |
| - | === NC3 – Crimson & Clover === | + | |
| - | Crimson & Clover ist der Titel [[https:// | + | |
| - | + | ||
| - | {{: | + | |
| - | + | ||
| - | + | ||
| - | == Schritt 1 – Herzblatt == | + | |
| - | <WRAP group> | + | |
| - | <WRAP column half> | + | |
| - | Schreibe eine Funktion '' | + | |
| - | * Das Argument '' | + | |
| - | * Das Argument '' | + | |
| - | * Verwende die Turtle-Funktionen '' | + | |
| - | * Teste die Funktion. | + | |
| - | + | ||
| - | </ | + | |
| - | <WRAP column quater> | + | |
| - | + | ||
| - | {{: | + | |
| - | + | ||
| - | </ | + | |
| - | <WRAP column quater> | + | |
| - | {{: | + | |
| - | + | ||
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | == Schritt 2 – Kleeblätter == | + | |
| - | <WRAP group> | + | |
| - | <WRAP column half> | + | |
| - | Schreibe eine weitere Funktionen '' | + | |
| - | * Verwende die eben erstellte Funktion herzblatt. | + | |
| - | * Das Argument '' | + | |
| - | * Teste die Funktion. | + | |
| - | + | ||
| - | Wiederhole Schritt 2 für eine Funktion '' | + | |
| - | + | ||
| - | </ | + | |
| - | <WRAP column quater> | + | |
| - | {{: | + | |
| - | </ | + | |
| - | <WRAP column quater> | + | |
| - | {{: | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | == Schritt 3 – Crimson & Clover == | + | |
| - | <WRAP group> | + | |
| - | <WRAP column half> | + | |
| - | Schreibe ein Programm, das die Kleeblatt-Funktionen verwendet und eine zufällige Wiese aus ingesamt 30 Purpur-Punkten und Kleeblättern malt, ähnlich dem Bild rechts: | + | |
| - | * Jede siebte Figur ist ein vierblättriges Kleeblatt. | + | |
| - | * Jede dritte Figur ist dreiblättriges Kleeblatt. | + | |
| - | * Alle anderen Figuren sind Purpur-Punkte. | + | |
| - | * Die Grösse jedes Kleeblatts oder Punktes wird jeweils zufällig bestimmt. | + | |
| - | * Beachte die Hinweise in Aufgabe NA4. | + | |
| - | </ | + | |
| - | + | ||
| - | <WRAP column half> | + | |
| - | {{: | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | == Zusatzaufgabe (Optional) == | + | |
| - | <WRAP group> | + | |
| - | <WRAP column half> | + | |
| - | + | ||
| - | Verbessere deine Herzblatt-Funktion: | + | |
| - | * Um ein Herz auf diese Weise zu zeichnen, benötigst du die Funktion '' | + | |
| - | <code python> | + | |
| - | import math | + | |
| - | print(math.sqrt(4)) | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | <WRAP column half> | + | |
| - | {{: | + | |
| - | </ | + | |
| - | </ | + | |
| - | === NC4 – Text rückwärts === | + | |
| - | + | ||
| - | <box 100% round green|**Strings vs. Listen**> | + | |
| - | + | ||
| - | **Texte** werden in python als **Strings** (engl. " | + | |
| - | + | ||
| - | **Strings** haben gewisse Ähnlichkeit mit **Listen**: Auch in Listen sind mehrere Elemente aneinander gehängt. Hier vergleichen wir Listen mit Strings anhand einiger Befehle/ | + | |
| - | + | ||
| - | <WRAP group> | + | |
| - | <WRAP column half> | + | |
| - | Befehle mit Listen: | + | |
| - | <code python> | + | |
| - | my_list = [] # Leere Liste erstellen | + | |
| - | numbers = [3,9,27] # Liste mit Inhalt erstellen | + | |
| - | numbers.append(81) # Der Liste ein Element anfügen | + | |
| - | n = numbers[2] # Bestimmtes Element in Liste wählen | + | |
| - | print(n) # Gibt 27 aus | + | |
| - | </ | + | |
| - | + | ||
| - | </ | + | |
| - | <WRAP column half> | + | |
| - | Befehle mit Strings: | + | |
| - | <code python> | + | |
| - | my_text = "" | + | |
| - | name = " | + | |
| - | name = name + " Kant" # Dem Text einen Text anfügen | + | |
| - | b = name[5] # Bestimmten Buchstaben im Text wählen | + | |
| - | print(b) # Gibt ' | + | |
| - | </ | + | |
| - | + | ||
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | Die Funktion '' | + | |
| - | + | ||
| - | + | ||
| - | </ | + | |
| - | + | ||
| - | Schreibe eine Funktion '' | + | |
| - | * Zum Beispiel soll der Aufruf '' | + | |
| - | * **Verwende eine While-Schleife**, | + | |
| - | * Hinweis: Python böte mit der [[https:// | + | |
| - | * Teste deine Funktion. | + | |
| - | + | ||
| - | === NC5 – Abstände ersetzen === | + | |
| - | Schreibe eine Funktion '' | + | |
| - | * Hinweis: Mit eckigen Klammern können wir zwar einen Buchstaben aus einem String // | + | |
| - | + | ||
| - | + | ||
| - | === NC6 – Wörter rückwärts (Challenge) === | + | |
| - | Schreibe eine Funktion '' | + | |
| - | + | ||
| - | == Schritt 1 == | + | |
| - | Schreibe zuerst eine Funktion '' | + | |
| - | + | ||
| - | == Schritt 2 == | + | |
| - | Schreibe nun die Funktion '' | + | |
| - | + | ||
| - | ===== - Aufgaben O – Funktionen für Fortgeschrittene ===== | + | |
| - | === O1 – Weitere Nächte im Hotel === | + | |
| - | + | ||
| - | <WRAP group> | + | |
| - | <WRAP column twothirds> | + | |
| - | Schreibe eine neue Funktion '' | + | |
| - | Preise **pro Nacht**: | + | |
| - | ^ Tarif (Anreisetag) | + | |
| - | | Sonntag bis Donnerstag | + | |
| - | | Freitag und Samstag | + | |
| - | + | ||
| - | </ | + | |
| - | <WRAP column third> | + | |
| - | {{: | + | |
| - | </ | + | |
| - | </ | + | |
| - | **Hinweise: | + | |
| - | * Deine Funktion muss zuerst die Anzahl Einzel- und Doppelzimmer berechnen (ausgehend von der Anzahl Personen): Es ist maximal ein Einzelzimmer nötig. | + | |
| - | * Die Angabe des Anreisetags erfolgt als Text, z. B. " | + | |
| - | * Erstelle in der Funktion eine Liste '' | + | |
| - | * Wenn du die Position des Anreisetag weisst, kannst du für jede Nacht beurteilen, ob Wochenendtarif gilt oder nicht – aber wie? Versuche es erst ohne Hilfe von KI. | + | |
| - | + | ||
| - | Teste deine Funktion mit folgendem Code: | + | |
| - | <code python> | + | |
| - | # Reise 1: 2 Personen, 2 Nächte ab Freitag: Preis = 400 | + | |
| - | print(hotel_preise2(2, | + | |
| - | # Reise 2: 3 Personen, 4 Nächte ab Mittwoch: Preis = 1220 | + | |
| - | print(hotel_preise2(3, | + | |
| - | # Reise 3: 1 Person, 10 Nächte ab Donnerstag: Preis = 1320 | + | |
| - | print(hotel_preise2(1, | + | |
| - | </ | + | |
| - | + | ||
| - | === O2 – RGB-Palette === | + | |
| - | - Schreibe eine Funktion '' | + | |
| - | * Die drei Argumente können jeweils einen Wert zwischen 0 und 5 einnehmen: | + | |
| - | * '' | + | |
| - | * '' | + | |
| - | * '' | + | |
| - | - Ergänze einen Code, der alle 216 Farben, die mit dieser Funktion gezeichnet werden können, in einer Farbpallete wie im Bild unten aufzeichnet. | + | |
| - | {{: | + | |
| - | * In jedem der sechs Quadrate nimmt der Blau-Anteil jeweils von links nach rechts und der Grün-Anteil von oben nach unten zu. | + | |
| - | * Über die sechs Quadrate hinweg nimmt der Rot-Anteil von links nach rechts zu. | + | |
| - | + | ||
| - | === O3 – Fibonacci-Spirale === | + | |
| - | <WRAP group> | + | |
| - | <WRAP column twothirds> | + | |
| - | Infomiere dich zuerst über die Informiere dich über die [[https:// | + | |
| - | - Erstelle eine Funktion '' | + | |
| - | - Erstelle eine Funktion '' | + | |
| - | - Erstelle eine Funktion '' | + | |
| - | </ | + | |
| - | <WRAP column third> | + | |
| - | {{: | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | <WRAP group> | + | |
| - | <WRAP column twothirds> | + | |
| - | == Zusatzaufbabe == | + | |
| - | Erstelle eine neue Funktion '' | + | |
| - | <code python> | + | |
| - | farben = [" | + | |
| - | fibo_spirale2(9, | + | |
| - | </ | + | |
| - | </ | + | |
| - | <WRAP column third> | + | |
| - | {{: | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | + | ||
| - | === O4 – Siebensegment-Anzeige === | + | |
| - | <WRAP group> | + | |
| - | <WRAP column twothirds> | + | |
| - | Die [[https:// | + | |
| - | + | ||
| - | {{: | + | |
| - | </ | + | |
| - | <WRAP column third> | + | |
| - | {{: | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | <WRAP group> | + | |
| - | <WRAP column twothirds> | + | |
| - | == Schritt 1 – Einzelnes Segment zeichnen == | + | |
| - | Erstelle eine Funktion '' | + | |
| - | * Das Argument '' | + | |
| - | * Das Argument '' | + | |
| - | * Starte bei der einen Spitze und gehe davon aus, dass die Turtle in Richtung der anderen Spitze schaut, siehe Bild. | + | |
| - | </ | + | |
| - | <WRAP column third> | + | |
| - | {{: | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | <WRAP group> | + | |
| - | <WRAP column twothirds> | + | |
| - | == Schritt 2 – Codes definieren == | + | |
| - | Im Bild rechts sind die 7 Segmente mit den Buchstaben A bis G bezeichnet. Den Dezimalpunkt lassen wir ausser Acht. Für die Ziffern **0** und **4** müssten die Segmente gemäss folgender Tabelle leuchten: | + | |
| - | ^ Ziffer | + | |
| - | | 0 | + | |
| - | | 4 | + | |
| - | + | ||
| - | * Erstelle eine Liste '' | + | |
| - | + | ||
| - | == Schritt 3 – Zahl mit 7 Segmenten anzeigen == | + | |
| - | Erstelle eine Funktion '' | + | |
| - | * Verwende die bereits erstellte Funktion '' | + | |
| - | * Speichere für jedes Segment die x-Positionen, | + | |
| - | * Das Argument number gibt an, welche Ziffer dargestellt werden soll. Verwende deine Liste '' | + | |
| - | * Teste deine Funktion mit einem Programm, dass die Zahlen von 0 bis 9 im Sekundentakt ('' | + | |
| - | </ | + | |
| - | <WRAP column third> | + | |
| - | {{: | + | |
| - | </ | + | |
| - | </ | + | |
| - | + | ||
| - | == Zusatzaufgaben == | + | |
| - | * Programmiere eine zweistellige Anzeige, die von 0 bis 99 zählen kann. | + | |
| - | * Programmiere eine digitale Uhr, die die aktuelle Zeit in Stunden, Minuten und Sekunden (von Doppelpunkten getrennt) anzeigt. | + | |
| - | + | ||
| - | ===== - Lösungen ===== | + | |
| - | + | ||
| - | **Achtung: | + | |
| <nodisp 1> | <nodisp 1> | ||
| Zeile 781: | Zeile 291: | ||
| </ | </ | ||
| - | <nodisp 1> | ||
| - | ++++Lösungen zu Aufgaben NA:| | ||
| - | === NA1 === | ||
| - | <code python> | ||
| - | from gturtle import * | ||
| - | flori = Turtle() | ||
| - | |||
| - | def blume(): | ||
| - | flori.setPenColor(" | ||
| - | i = 0 | ||
| - | while i < 6: | ||
| - | flori.rightArc(20, | ||
| - | flori.dot(30) | ||
| - | i = i + 1 | ||
| - | |||
| - | blume() | ||
| - | flori.setPos(60, | ||
| - | blume() | ||
| - | flori.setPos(100, | ||
| - | blume() | ||
| - | </ | ||
| - | |||
| - | === NA2 === | ||
| - | <code python> | ||
| - | from gturtle import * | ||
| - | flori = Turtle() | ||
| - | |||
| - | def blume(color): | ||
| - | flori.setPenColor(color) | ||
| - | i = 0 | ||
| - | while i < 6: | ||
| - | flori.rightArc(20, | ||
| - | flori.dot(30) | ||
| - | i = i + 1 | ||
| - | |||
| - | blume(" | ||
| - | flori.setPos(60, | ||
| - | blume(" | ||
| - | flori.setPos(80, | ||
| - | blume(" | ||
| - | flori.setPos(140, | ||
| - | blume(" | ||
| - | </ | ||
| - | |||
| - | === NA3 – Schritt 1 === | ||
| - | <code python> | ||
| - | from gturtle import * | ||
| - | flori = Turtle() | ||
| - | |||
| - | def blume(color, | ||
| - | flori.setPenColor(color) | ||
| - | flori.setPos(xpos, | ||
| - | i = 0 | ||
| - | while i < 6: | ||
| - | flori.rightArc(20, | ||
| - | flori.dot(30) | ||
| - | i = i + 1 | ||
| - | x = 0 | ||
| - | while x < 600: | ||
| - | blume(" | ||
| - | x = x + 100 | ||
| - | </ | ||
| - | |||
| - | === NA3 – Schritt 2 === | ||
| - | <code python> | ||
| - | from gturtle import * | ||
| - | flori = Turtle() | ||
| - | |||
| - | def blume(color, | ||
| - | flori.setPenColor(color) | ||
| - | flori.setPos(xpos, | ||
| - | i = 0 | ||
| - | while i < 6: | ||
| - | flori.rightArc(20, | ||
| - | flori.dot(30) | ||
| - | i = i + 1 | ||
| - | |||
| - | colors = [" | ||
| - | xpositions = [0, | ||
| - | ypositions = [0, | ||
| - | |||
| - | i = 0 | ||
| - | while i < 6: | ||
| - | blume(colors[i], | ||
| - | i = i + 1 | ||
| - | </ | ||
| - | |||
| - | === NA4 === | ||
| - | <code python> | ||
| - | from gturtle import * | ||
| - | flori = Turtle() | ||
| - | flori.hideTurtle() | ||
| - | flori.clear(" | ||
| - | |||
| - | def blume(color, | ||
| - | flori.setPenColor(color) | ||
| - | flori.setPos(xpos, | ||
| - | i = 0 | ||
| - | while i < 6: | ||
| - | flori.rightArc(20, | ||
| - | flori.dot(30) | ||
| - | i = i + 1 | ||
| - | |||
| - | colors = [" | ||
| - | |||
| - | i = 0 | ||
| - | while i < 20: | ||
| - | col_pos = random.randint(0, | ||
| - | blume(colors[col_pos], | ||
| - | i = i + 1 | ||
| - | </ | ||
| - | ++++ | ||
| - | </ | ||
| - | |||
| - | <nodisp 1> | ||
| - | ++++Lösungen zu Aufgaben NB:| | ||
| - | === NB1 === | ||
| - | <code python> | ||
| - | def chf_in_nok(chf): | ||
| - | return chf * 12.57 | ||
| - | |||
| - | print(chf_in_nok(3)) | ||
| - | </ | ||
| - | |||
| - | === NB2 === | ||
| - | <code python> | ||
| - | def max_von_zwei(a, | ||
| - | if a > b: | ||
| - | return a | ||
| - | else: | ||
| - | return b | ||
| - | |||
| - | print(max_von_zwei(2, | ||
| - | print(max_von_zwei(3, | ||
| - | </ | ||
| - | |||
| - | === NB3 === | ||
| - | <code python> | ||
| - | def zutaten_pfannkuchen(n): | ||
| - | return n * 50, n * 0.125, n, n | ||
| - | |||
| - | n = input(" | ||
| - | mehl, salz, milch, ei = zutaten_pfannkuchen(n) | ||
| - | print(f" | ||
| - | </ | ||
| - | |||
| - | === NB4 === | ||
| - | <code python> | ||
| - | def min_von_drei(a, | ||
| - | if a < b and a < c: | ||
| - | return a | ||
| - | elif b < a and b < c: | ||
| - | return b | ||
| - | else: | ||
| - | return c | ||
| - | |||
| - | print(min_von_drei(1, | ||
| - | print(min_von_drei(5, | ||
| - | print(min_von_drei(9, | ||
| - | </ | ||
| - | |||
| - | === NB5 === | ||
| - | <code python> | ||
| - | list1 = [11, 16, 5, 12, 3, 13] | ||
| - | list2 = [13, 2, 5, 10, 1, 17, 5, 25] | ||
| - | list3 = [-25, -13, -3, -33] | ||
| - | |||
| - | def max_in_liste(liste): | ||
| - | max_wert = liste[0] | ||
| - | i = 1 | ||
| - | while i < len(liste): | ||
| - | if liste[i] > max_wert: | ||
| - | max_wert = liste[i] | ||
| - | i = i + 1 | ||
| - | return max_wert | ||
| - | |||
| - | print(max_in_liste(list1)) | ||
| - | print(max_in_liste(list2)) | ||
| - | print(max_in_liste(list3)) | ||
| - | </ | ||
| - | |||
| - | === NB6 === | ||
| - | <code python> | ||
| - | def ist_gerade(n): | ||
| - | if n % 2 == 0: | ||
| - | return True | ||
| - | return False | ||
| - | |||
| - | print(ist_gerade(4)) | ||
| - | print(ist_gerade(5)) | ||
| - | </ | ||
| - | |||
| - | === NB7 === | ||
| - | <code python> | ||
| - | def pizza_lieferung(personen): | ||
| - | if personen * 12 >= 50: | ||
| - | return personen * 12 | ||
| - | else: | ||
| - | return personen * 12 + 20 | ||
| - | |||
| - | print(pizza_lieferung(4)) | ||
| - | print(pizza_lieferung(5)) | ||
| - | </ | ||
| - | |||
| - | === NB8 === | ||
| - | <code python> | ||
| - | |||
| - | </ | ||
| - | ++++ | ||
| - | </ | ||
| - | |||
| - | <nodisp 1> | ||
| - | ++++Lösungen zu Aufgaben NC:| | ||
| - | |||
| - | === NC1 === | ||
| - | <code python> | ||
| - | from gturtle import* | ||
| - | karl = Turtle() | ||
| - | |||
| - | def hexagon(): | ||
| - | side_count = 0 | ||
| - | while side_count < 6: | ||
| - | karl.forward(50) | ||
| - | karl.right(60) | ||
| - | side_count = side_count + 1 | ||
| - | |||
| - | hex_count = 0 | ||
| - | while hex_count < 6: | ||
| - | hexagon() | ||
| - | karl.forward(50) | ||
| - | karl.left(60) | ||
| - | hex_count = hex_count + 1 | ||
| - | </ | ||
| - | === NC2 === | ||
| - | <code python> | ||
| - | import random | ||
| - | from gturtle import * | ||
| - | flori = Turtle() | ||
| - | |||
| - | def punkte(min, max, colors): | ||
| - | i = 0 | ||
| - | anz = random.randint(min, | ||
| - | while i < anz: | ||
| - | xpos = random.randint(-250, | ||
| - | ypos = random.randint(-100, | ||
| - | flori.setPenColor(colors[random.randint(0, | ||
| - | flori.setPos(xpos, | ||
| - | flori.dot(20) | ||
| - | i = i + 1 | ||
| - | return i | ||
| - | |||
| - | farben = [" | ||
| - | |||
| - | anz_punkte = punkte(5, 40, farben) | ||
| - | print(f" | ||
| - | </ | ||
| - | |||
| - | === NC3 === | ||
| - | <code python> | ||
| - | import random | ||
| - | from gturtle import * | ||
| - | tommy = Turtle() | ||
| - | tommy.hideTurtle() | ||
| - | tommy.clear(" | ||
| - | |||
| - | def herzblatt(seite, | ||
| - | tommy.setFillColor(farbe) | ||
| - | tommy.startPath() | ||
| - | tommy.forward(seite) | ||
| - | tommy.rightArc(seite/ | ||
| - | tommy.left(90) | ||
| - | tommy.rightArc(seite/ | ||
| - | tommy.forward(seite) | ||
| - | tommy.fillPath() | ||
| - | tommy.right(90) | ||
| - | |||
| - | def kleeblatt3(radius): | ||
| - | i = 0 | ||
| - | while i < 3: | ||
| - | herzblatt(radius, | ||
| - | tommy.right(120) | ||
| - | i = i + 1 | ||
| - | |||
| - | def kleeblatt4(radius): | ||
| - | i = 0 | ||
| - | while i < 4: | ||
| - | herzblatt(radius, | ||
| - | tommy.right(90) | ||
| - | i = i + 1 | ||
| - | | ||
| - | i = 1 | ||
| - | while i < 30: | ||
| - | xpos = random.randint(-250, | ||
| - | ypos = random.randint(-100, | ||
| - | size = random.randint(20, | ||
| - | tommy.setPos(xpos, | ||
| - | |||
| - | if i % 7 == 0: | ||
| - | kleeblatt4(size) | ||
| - | elif i % 3 == 0: | ||
| - | kleeblatt3(size) | ||
| - | else: | ||
| - | tommy.setPenColor(" | ||
| - | tommy.dot(size) | ||
| - | | ||
| - | i = i + 1 | ||
| - | </ | ||
| - | |||
| - | === NC3 Zusatzaufgabe=== | ||
| - | <code python> | ||
| - | import random | ||
| - | import math | ||
| - | from gturtle import * | ||
| - | tommy = Turtle() | ||
| - | tommy.hideTurtle() | ||
| - | tommy.clear(" | ||
| - | |||
| - | def herzblatt2(seite, | ||
| - | tommy.setFillColor(farbe1) | ||
| - | tommy.startPath() | ||
| - | tommy.forward(seite) | ||
| - | tommy.rightArc(seite/ | ||
| - | tommy.right(45) | ||
| - | tommy.forward(seite*math.sqrt(2)) | ||
| - | tommy.fillPath() | ||
| - | tommy.setFillColor(farbe2) | ||
| - | tommy.startPath() | ||
| - | tommy.left(135) | ||
| - | tommy.forward(seite) | ||
| - | tommy.leftArc(seite/ | ||
| - | tommy.left(45) | ||
| - | tommy.forward(seite*math.sqrt(2)) | ||
| - | tommy.fillPath() | ||
| - | | ||
| - | def kleeblatt3(radius): | ||
| - | i = 0 | ||
| - | while i < 3: | ||
| - | herzblatt2(radius, | ||
| - | tommy.right(15) | ||
| - | i = i + 1 | ||
| - | |||
| - | def kleeblatt4(radius): | ||
| - | i = 0 | ||
| - | while i < 4: | ||
| - | herzblatt2(radius, | ||
| - | tommy.right(45) | ||
| - | i = i + 1 | ||
| - | |||
| - | i = 1 | ||
| - | while i < 30: | ||
| - | xpos = random.randint(-250, | ||
| - | ypos = random.randint(-100, | ||
| - | size = random.randint(20, | ||
| - | tommy.setPos(xpos, | ||
| - | |||
| - | if i % 7 == 0: | ||
| - | kleeblatt4(size) | ||
| - | elif i % 3 == 0: | ||
| - | kleeblatt3(size) | ||
| - | else: | ||
| - | tommy.setPenColor(" | ||
| - | tommy.dot(size) | ||
| - | | ||
| - | i = i + 1 | ||
| - | </ | ||
| - | |||
| - | === NC4 === | ||
| - | <WRAP group> | ||
| - | <WRAP column half> | ||
| - | Variante mit While-Schleife: | ||
| - | |||
| - | <code python> | ||
| - | def text_reverse(text): | ||
| - | new_text = "" | ||
| - | i = len(text)-1 | ||
| - | while i >= 0: | ||
| - | new_text = new_text + text[i] | ||
| - | i = i-1 | ||
| - | return new_text | ||
| - | | ||
| - | print(text_reverse(" | ||
| - | </ | ||
| - | </ | ||
| - | <WRAP column half> | ||
| - | Kurze Variante mit Slice-Syntax (optional): | ||
| - | |||
| - | <code python> | ||
| - | def text_reverse(text): | ||
| - | return text[::-1] | ||
| - | | ||
| - | print(text_reverse(" | ||
| - | </ | ||
| - | </ | ||
| - | </ | ||
| - | |||
| - | |||
| - | === NC5 === | ||
| - | <code python> | ||
| - | def replace_spaces(text, | ||
| - | i = 0 | ||
| - | new_text = "" | ||
| - | while i < len(text): | ||
| - | if text[i] == ' ': | ||
| - | new_text = new_text + char | ||
| - | else: | ||
| - | new_text = new_text + text[i] | ||
| - | i = i + 1 | ||
| - | return new_text | ||
| - | |||
| - | print(replace_spaces(" | ||
| - | </ | ||
| - | |||
| - | === NC6 === | ||
| - | <code python> | ||
| - | def word_list(text): | ||
| - | words = [] | ||
| - | word = "" | ||
| - | i = 0 | ||
| - | while i < len(text): | ||
| - | if text[i] == ' ': # Leerzeichen | ||
| - | words.append(word) | ||
| - | word = "" | ||
| - | else: | ||
| - | word = word + text[i] | ||
| - | i = i + 1 | ||
| - | words.append(word) | ||
| - | return words | ||
| - | |||
| - | def words_reverse(text): | ||
| - | words = word_list(text) | ||
| - | new_text = "" | ||
| - | i = len(words) - 1 | ||
| - | while i >= 0: | ||
| - | new_text = new_text + words[i] + " " | ||
| - | i = i - 1 | ||
| - | return new_text | ||
| - | |||
| - | print(words_reverse(" | ||
| - | </ | ||
| - | ++++ | ||
| - | </ | ||
| - | |||
| - | <nodisp 1> | ||
| - | ++++Lösungen zu Aufgaben O:| | ||
| - | |||
| - | === O1 === | ||
| - | <code python> | ||
| - | def hotel_preise2(personen, | ||
| - | doppelzimmer = personen // 2 | ||
| - | einzelzimmer = personen % 2 | ||
| - | tage = [" | ||
| - | starttag = tage.index(anreisetag) | ||
| - | preis = 0 | ||
| - | nacht = 0 | ||
| - | | ||
| - | while nacht < naechte: | ||
| - | aktueller_tag_index = (starttag + nacht) % 7 | ||
| - | aktueller_tag = tage[aktueller_tag_index] | ||
| - | | ||
| - | if aktueller_tag in [" | ||
| - | preis = preis + doppelzimmer * 200 + einzelzimmer * 150 | ||
| - | else: | ||
| - | preis = preis + doppelzimmer * 140 + einzelzimmer * 120 | ||
| - | | ||
| - | nacht = nacht + 1 | ||
| - | | ||
| - | return preis | ||
| - | |||
| - | # Reise 1: 2 Personen, 2 Nächte ab Freitag: Preis = 400 | ||
| - | print(hotel_preise2(2, | ||
| - | # Reise 2: 3 Personen, 4 Nächte ab Mittwoch: Preis = 1220 | ||
| - | print(hotel_preise2(3, | ||
| - | # Reise 3: 1 Person, 10 Nächte ab Donnerstag: Preis = 1320 | ||
| - | print(hotel_preise2(1, | ||
| - | </ | ||
| - | |||
| - | === O2 === | ||
| - | <code python> | ||
| - | from gturtle import * | ||
| - | paula = Turtle() | ||
| - | |||
| - | def rgb_dot(red, | ||
| - | hexcodes = [" | ||
| - | colorcode = hexcodes[red] + hexcodes[green] + hexcodes[blue] | ||
| - | paula.setPenColor(colorcode) | ||
| - | paula.dot(40) | ||
| - | | ||
| - | paula.right(90) | ||
| - | xpos = 0 | ||
| - | ypos = 0 | ||
| - | |||
| - | for r in range(6): | ||
| - | |||
| - | for g in range(6): | ||
| - | | ||
| - | for b in range(6): | ||
| - | paula.setPos(xpos, | ||
| - | rgb_dot(r, | ||
| - | xpos = xpos + 50 | ||
| - | | ||
| - | xpos = xpos - 300 | ||
| - | ypos = ypos - 50 | ||
| - | | ||
| - | xpos = xpos + 350 | ||
| - | ypos = 0 | ||
| - | </ | ||
| - | |||
| - | === O3 === | ||
| - | <code python> | ||
| - | from gturtle import * | ||
| - | leonardo = Turtle() | ||
| - | |||
| - | def fibonacci(n): | ||
| - | fibs = [] | ||
| - | s1 = 0 | ||
| - | s2 = 1 | ||
| - | i = 0 | ||
| - | while i < n: | ||
| - | fibs.append(s1) | ||
| - | s = s1 + s2 | ||
| - | s1 = s2 | ||
| - | s2 = s | ||
| - | i = i + 1 | ||
| - | return fibs | ||
| - | |||
| - | def viertelkreis(radius, | ||
| - | leonardo.setFillColor(farbe) | ||
| - | leonardo.startPath() | ||
| - | leonardo.rightArc(radius, | ||
| - | leonardo.right(90) | ||
| - | leonardo.forward(radius) | ||
| - | leonardo.fillPath() | ||
| - | leonardo.right(180) | ||
| - | leonardo.forward(radius) | ||
| - | leonardo.right(90) | ||
| - | |||
| - | def fibo_spirale(n): | ||
| - | fibos = fibonacci(n) | ||
| - | i = 0 | ||
| - | while i < len(fibos): | ||
| - | radius = fibos[i] * 10 | ||
| - | if i % 2 == 0: | ||
| - | viertelkreis(radius," | ||
| - | else: | ||
| - | viertelkreis(radius," | ||
| - | i = i + 1 | ||
| - | |||
| - | def fibo_spirale2(n, | ||
| - | fibos = fibonacci(n) | ||
| - | i = 0 | ||
| - | while i < len(fibos): | ||
| - | radius = fibos[i] * 10 | ||
| - | viertelkreis(radius, | ||
| - | i = i + 1 | ||
| - | |||
| - | leonardo.right(90) | ||
| - | farben = [" | ||
| - | fibo_spirale2(9, | ||
| - | </ | ||
| - | |||
| - | === O4 === | ||
| - | <code python> | ||
| - | import math | ||
| - | from gturtle import * | ||
| - | |||
| - | carl = Turtle() | ||
| - | carl.hideTurtle() | ||
| - | |||
| - | def segment(length, | ||
| - | l1 = length * 0.8 | ||
| - | l2 = (length - l1)/2 | ||
| - | side = l2 * math.sqrt(2) | ||
| - | carl.setFillColor(color) | ||
| - | carl.startPath() | ||
| - | carl.right(45) | ||
| - | repeat 2: | ||
| - | carl.forward(side) | ||
| - | carl.left(45) | ||
| - | carl.forward(l1) | ||
| - | carl.left(45) | ||
| - | carl.forward(side) | ||
| - | carl.left(90) | ||
| - | carl.fillPath() | ||
| - | carl.left(45) | ||
| - | |||
| - | def seven_seg(number): | ||
| - | segment_codes = [' | ||
| - | ' | ||
| - | ' | ||
| - | ' | ||
| - | ' | ||
| - | ' | ||
| - | ' | ||
| - | ' | ||
| - | ' | ||
| - | ' | ||
| - | |||
| - | xpositions = [0, 100, 100, 100, 0, 0, 0] | ||
| - | ypositions = [0, 0, -100, -200, -200, -100, -100] | ||
| - | headings = [90, 180, 180, 270, 0, 0, 90] | ||
| - | | ||
| - | i = 0 | ||
| - | for s in segment_codes[number]: | ||
| - | carl.setPos(xpositions[i], | ||
| - | carl.setHeading(headings[i]) | ||
| - | if s == ' | ||
| - | segment(100, | ||
| - | else: | ||
| - | segment(100, | ||
| - | i = i + 1 | ||
| - | |||
| - | i = 0 | ||
| - | while i <= 9: | ||
| - | seven_seg(i) | ||
| - | i = i + 1 | ||
| - | delay(1000) | ||
| - | </ | ||
| - | ++++ | ||
| - | </ | ||