Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
| Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
| gf_informatik:funktionen [2026-04-28 19:49] – [Aufgabe E4] hof | gf_informatik:funktionen [2026-04-28 20:19] (aktuell) – [Aufgabe F6 (optional)] hof | ||
|---|---|---|---|
| Zeile 224: | Zeile 224: | ||
| === Aufgabe E5 === | === Aufgabe E5 === | ||
| + | < | ||
| + | <div slot=" | ||
| Schreibe folgende Funktionen: | Schreibe folgende Funktionen: | ||
| - | * `square(x, | + | < |
| - | * `circle(x, | + | < |
| - | * `rectange(x, | + | < |
| - | * `triangle(x, | + | < |
| - | * Funktion für selbst gewählte geometrische Figur | + | < |
| + | <li>Funktion für selbst gewählte geometrische Figur | ||
| + | </ | ||
| + | < | ||
| - | Mache nun moderne Kunst, indem du diese Funktionen aufrufst. | + | < |
| - | + | ||
| - | **Challenge (optional):** Erstelle mit möglichst vielen Zufallswerten moderne Kunst. Ziel: Kunst von deinem Programm soll besser und schöner sein als die vom Programm der Lehrperson! | + | |
| - | + | ||
| - | Vom Zufall abhängen können z.B. folgende Werte: | + | |
| - | * Anzahl, wie oft jede Funktion aufgerufen wird. | + | |
| - | * Position von Figuren | + | |
| - | * Grössen von Figuren | + | |
| - | * Drehung von Figuren | + | |
| - | * Farben (Stift- und Füllfarbe), | + | |
| + | < | ||
| + | <ul> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ul> | ||
| Tipps: | Tipps: | ||
| - | | + | < |
| - | * Du kannst auch weitere Funktionen definieren, die dir das Leben erleichtern. | + | < |
| - | + | <li>Du kannst auch weitere Funktionen definieren, die dir das Leben erleichtern. | |
| - | <nodisp 1> | + | </ul> |
| - | ++++Lösungen Aufgaben E| | + | </div> |
| - | + | <template data-type="solution"> | |
| - | ==== Aufgaben E ==== | + | |
| - | + | ||
| - | === Aufgabe E1 === | + | |
| - | + | ||
| - | < | + | |
| - | def greetings(name, | + | |
| - | print(f" | + | |
| - | + | ||
| - | greetings(" | + | |
| - | greetings(" | + | |
| - | </bottom-editor> | + | |
| - | + | ||
| - | === Aufgabe E2 === | + | |
| - | + | ||
| - | < | + | |
| - | import random | + | |
| - | + | ||
| - | # Wichtig: Die Ermittlung der Zufallszahl mit randint und die | + | |
| - | # Verzweigung (Kopf oder Zahl) muss IN der Funktion gemacht | + | |
| - | # werden, nicht ausserhalb! | + | |
| - | + | ||
| - | def head_or_tail(): | + | |
| - | r = random.randint(1, | + | |
| - | if r == 1: | + | |
| - | print(" | + | |
| - | else: | + | |
| - | print(" | + | |
| - | + | ||
| - | head_or_tail() | + | |
| - | head_or_tail() | + | |
| - | </bottom-editor> | + | |
| - | + | ||
| - | === Aufgabe E3 === | + | |
| - | + | ||
| - | < | + | |
| - | import random | + | |
| - | + | ||
| - | def fortune_cookie(): | + | |
| - | r = random.randint(1, | + | |
| - | if r == 1: | + | |
| - | print(" | + | |
| - | elif r == 2: | + | |
| - | print(" | + | |
| - | else: | + | |
| - | print(" | + | |
| - | + | ||
| - | fortune_cookie() | + | |
| - | </ | + | |
| - | + | ||
| - | === Aufgabe E4 === | + | |
| - | + | ||
| - | < | + | |
| - | from turtle import * | + | |
| - | turi = Turtle() | + | |
| - | turi.hideturtle() | + | |
| - | turi.speed(1000) | + | |
| - | + | ||
| - | def square(l): | + | |
| - | i = 0 | + | |
| - | while i < 4: | + | |
| - | turi.forward(l) | + | |
| - | turi.right(90) | + | |
| - | i = i + 1 | + | |
| - | + | ||
| - | square(50) | + | |
| - | square(100) | + | |
| - | square(150) | + | |
| - | square(200) | + | |
| - | </ | + | |
| - | + | ||
| - | === Aufgabe E5 === | + | |
| - | + | ||
| - | < | + | |
| - | from turtle import * | + | |
| - | t = Turtle() | + | |
| - | + | ||
| - | def square(x, | + | |
| - | t.teleport(x, | + | |
| - | i = 0 | + | |
| - | while i < 4: | + | |
| - | t.fd(l) | + | |
| - | t.right(90) | + | |
| - | i = i + 1 | + | |
| - | + | ||
| - | def circle(x, | + | |
| - | t.teleport(x, | + | |
| - | t.circle(r) | + | |
| - | + | ||
| - | def rectangle(x, | + | |
| - | t.teleport(x, | + | |
| - | i = 0 | + | |
| - | while i < 2: | + | |
| - | t.fd(a) | + | |
| - | t.right(90) | + | |
| - | t.fd(b) | + | |
| - | t.right(90) | + | |
| - | i = i + 1 | + | |
| - | + | ||
| - | def triangle(x, | + | |
| - | t.teleport(x, | + | |
| - | i = 0 | + | |
| - | while i < 3: | + | |
| - | t.fd(l) | + | |
| - | t.right(120) | + | |
| - | i = i + 1 | + | |
| - | + | ||
| - | square(-200, | + | |
| - | circle(0, | + | |
| - | rectangle(-150, | + | |
| - | triangle(100, | + | |
| - | </ | + | |
| - | + | ||
| - | <bottom-editor layout="split"> | + | |
| import random | import random | ||
| from turtle import * | from turtle import * | ||
| Zeile 431: | Zeile 322: | ||
| artist(t) | artist(t) | ||
| - | </bottom-editor> | + | </template> |
| - | + | </bottom-exercise> | |
| - | ++++ | + | |
| - | </nodisp> | + | |
| ==== Aufgaben F ==== | ==== Aufgaben F ==== | ||
| Zeile 449: | Zeile 338: | ||
| === Aufgabe F1 === | === Aufgabe F1 === | ||
| - | Definiere eine Funktion mit einem Argument `volume_cube(x)`, | + | < |
| + | <div slot=" | ||
| + | Schreibe folgende Funktionen: | ||
| - | Welches Volumen hat ein Würfel mit Seitenlänge 13 cm? | + | < |
| - | + | ||
| - | === Aufgabe F2 === | + | |
| - | + | ||
| - | **Satz von Pythagoras: | + | |
| - | + | ||
| - | //Tipp:// Die Wurzel einer Zahl ziehst du mit `sqrt(x)`, dazu musst du aber zuerst das math-Modul importieren: | + | |
| - | + | ||
| - | // | + | |
| - | + | ||
| - | + | ||
| - | === Aufgabe F3 === | + | |
| - | + | ||
| - | Das Volumen einer Kugel mit Radius $R$ ist: $$V = \frac{4\pi}{3}\cdot R^3$$ | + | |
| - | Definiere eine Funktion `volume_sphere(...)`, | + | |
| - | + | ||
| - | === Aufgabe F4 === | + | |
| - | + | ||
| - | Schreibe eine Funktion `grade(points)`, | + | |
| - | $$\text{Note} = \frac{5 \cdot \text{(erreichte Punkte)}}{\text{Punktzahl für Note 6}} + 1$$ | + | |
| - | + | ||
| - | Beachte: | + | |
| - | * Um die Noten schön zu runden, kannst du die // | + | |
| - | * Erreicht man mehr Punkte als notwendig für Note 6, soll man trotzdem die Note 6 erhalten. | + | |
| - | + | ||
| - | === Aufgabe F5 === | + | |
| - | + | ||
| - | Die **Fakultät** ist eine Funktion, welche jeder ganzen natürlichen Zahl das Produkt aller natürlichen Zahlen (ohne Null) kleiner und gleich dieser Zahl zuordnet. Sie wird mit einem Ausrufezeichen geschrieben. Als Beispiel: $5!=1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 =120$. | + | |
| - | + | ||
| - | Schreibe eine Funktion `factorial(...)`, | + | |
| - | + | ||
| - | **optionale Challenge für absolute Freaks**: Kannst du die Fakultät ganz ohne Schleife berechnen? Dann hast du das Prinzip der *Rekursion* (Selbst-Aufruf) entdeckt! | + | |
| - | + | ||
| - | === Aufgabe F6 (optional) === | + | |
| - | + | ||
| - | **Mitternachtsformel: | + | |
| - | $$x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}$$ | + | |
| - | + | ||
| - | Schreibe eine Funktion `mitternachtsformel(...)`, | + | |
| - | * keine Lösung: gib `None` zurück, dies ist der Fall, wenn der Term in der Wurzel negativ ist | + | |
| - | * eine Lösung, dies ist der Fall, wenn der Term in der Wurzel genau 0 ist | + | |
| - | * zwei Lösungen: gib beide Werte (mit Komma getrennt) zurück | + | |
| - | + | ||
| - | Tipp: Verwende die Diskriminante, | + | |
| - | + | ||
| - | Kontrolle: Die quadratische Gleichung ... | + | |
| - | + | ||
| - | * $3 x^2 - 6 x - 5 = 0$ hat die zwei Lösungen: $-0.632993$ und $2.63299$ | + | |
| - | * $x^2 - 4 x + 4 = 0$ hat eine Lösung: $2$ | + | |
| - | * $x^2 + 2 x + 7 = 0$ hat keine Lösung | + | |
| - | + | ||
| - | <nodisp 1> | + | |
| - | + | ||
| - | ++++Lösungen Aufgaben F| | + | |
| - | + | ||
| - | ==== Aufgaben F ==== | + | |
| - | + | ||
| - | === Aufgabe F1 === | + | |
| - | <bottom-editor> | + | <p> |
| + | </ | ||
| + | < | ||
| def volume_cube(x): | def volume_cube(x): | ||
| return x**3 | return x**3 | ||
| Zeile 516: | Zeile 352: | ||
| v = volume_cube(13) | v = volume_cube(13) | ||
| print(v) | print(v) | ||
| - | </ | + | </ |
| + | < | ||
| + | assert volume_cube(13) == 2197 | ||
| + | assert volume_cube(4) == 64 | ||
| + | </ | ||
| + | </ | ||
| === Aufgabe F2 === | === Aufgabe F2 === | ||
| - | <bottom-editor> | + | <bottom-exercise id=" |
| + | <div slot=" | ||
| + | < | ||
| + | |||
| + | < | ||
| + | |||
| + | < | ||
| + | </ | ||
| + | < | ||
| import math | import math | ||
| def pythagoras(a, | def pythagoras(a, | ||
| Zeile 526: | Zeile 375: | ||
| print(pythagoras(3, | print(pythagoras(3, | ||
| - | </bottom-editor> | + | </template> |
| + | < | ||
| + | assert pythagoras(3, | ||
| + | assert pythagoras(8, | ||
| + | </ | ||
| + | </ | ||
| === Aufgabe F3 === | === Aufgabe F3 === | ||
| - | <bottom-editor> | + | Das Volumen einer Kugel mit Radius $R$ ist: $$V = \frac{4\pi}{3}\cdot R^3$$ |
| + | <bottom-exercise id=" | ||
| + | <div slot=" | ||
| + | Definiere eine Funktion < | ||
| + | </ | ||
| + | < | ||
| import math | import math | ||
| def volume_sphere(r): | def volume_sphere(r): | ||
| Zeile 537: | Zeile 395: | ||
| print(volume_sphere(3)) | print(volume_sphere(3)) | ||
| - | </bottom-editor> | + | </template> |
| + | < | ||
| + | assert 113.09 < volume_sphere(3) < 113.099 | ||
| + | </ | ||
| + | </ | ||
| === Aufgabe F4 === | === Aufgabe F4 === | ||
| - | <bottom-editor> | + | Schreibe eine Funktion `grade(points)`, |
| + | $$\text{Note} = \frac{5 \cdot \text{(erreichte Punkte)}}{\text{Punktzahl für Note 6}} + 1$$ | ||
| + | |||
| + | Beachte: | ||
| + | * Um die Noten schön zu runden, kannst du die // | ||
| + | * Erreicht man mehr Punkte als notwendig für Note 6, soll man trotzdem die Note 6 erhalten. | ||
| + | |||
| + | <bottom-exercise id=" | ||
| + | < | ||
| def grade(points, | def grade(points, | ||
| gr = 5*points/ | gr = 5*points/ | ||
| Zeile 550: | Zeile 419: | ||
| print(grade(23, | print(grade(23, | ||
| - | </ | + | </ |
| + | </ | ||
| === Aufgabe F5 === | === Aufgabe F5 === | ||
| - | <bottom-editor> | + | Die **Fakultät** ist eine Funktion, welche jeder ganzen natürlichen Zahl das Produkt aller natürlichen Zahlen (ohne Null) kleiner und gleich dieser Zahl zuordnet. Sie wird mit einem Ausrufezeichen geschrieben. Als Beispiel: $5!=1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 =120$. |
| + | |||
| + | <bottom-exercise id=" | ||
| + | <div slot=" | ||
| + | Schreibe eine Funktion < | ||
| + | |||
| + | < | ||
| + | </ | ||
| + | < | ||
| def factorial(n): | def factorial(n): | ||
| product = 1 | product = 1 | ||
| Zeile 565: | Zeile 442: | ||
| print(factorial(5)) | print(factorial(5)) | ||
| - | </ | + | </ |
| + | < | ||
| + | assert factorial(5) == 120 | ||
| + | assert factorial(10) == 3628800 | ||
| + | </ | ||
| + | </ | ||
| + | === Aufgabe F6 (optional) === | ||
| - | === Aufgabe F6 === | + | **Mitternachtsformel: |
| + | $$x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}$$ | ||
| + | |||
| + | Schreibe eine Funktion `mitternachtsformel(...)`, | ||
| + | * keine Lösung: gib `None` zurück, dies ist der Fall, wenn der Term in der Wurzel negativ ist | ||
| + | * eine Lösung, dies ist der Fall, wenn der Term in der Wurzel genau 0 ist | ||
| + | * zwei Lösungen: gib beide Werte (mit Komma getrennt) zurück | ||
| - | <bottom-editor> | + | Tipp: Verwende die Diskriminante, |
| + | |||
| + | Kontrolle: Die quadratische Gleichung ... | ||
| + | |||
| + | * $3 x^2 - 6 x - 5 = 0$ hat die zwei Lösungen: $-0.632993$ und $2.63299$ | ||
| + | * $x^2 - 4 x + 4 = 0$ hat eine Lösung: $2$ | ||
| + | * $x^2 + 2 x + 7 = 0$ hat keine Lösung | ||
| + | |||
| + | <bottom-exercise id=" | ||
| + | < | ||
| from math import * | from math import * | ||
| def mitternachtsformel(a, | def mitternachtsformel(a, | ||
| Zeile 585: | Zeile 483: | ||
| print(mitternachtsformel(1, | print(mitternachtsformel(1, | ||
| print(mitternachtsformel(1, | print(mitternachtsformel(1, | ||
| - | </bottom-editor> | + | </template> |
| - | + | < | |
| - | ++++ | + | assert set(mitternachtsformel(3, |
| + | assert mitternachtsformel(1, | ||
| + | assert mitternachtsformel(1, | ||
| + | </ | ||
| + | </ | ||
| - | </ | ||