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_ii_sca [2025-05-21 20:21] – [Aufgabe] sca | gf_informatik:programmieren_ii_sca [2025-05-27 09:15] (aktuell) – [Kapitel V: Modern Art] sca | ||
---|---|---|---|
Zeile 1536: | Zeile 1536: | ||
r = random.randint(10, | r = random.randint(10, | ||
</ | </ | ||
+ | |||
+ | ++++Lösung Modern Art mit Quadraten und Dreiecken| | ||
+ | |||
+ | <code python> | ||
+ | from gturtle import * | ||
+ | import random | ||
+ | |||
+ | turi = Turtle() | ||
+ | turi.hideTurtle() | ||
+ | |||
+ | colors = [' | ||
+ | # | ||
+ | |||
+ | def dreieck(x, | ||
+ | r_width = random.randint(3, | ||
+ | turi.setPenWidth(r_width) | ||
+ | | ||
+ | r_pen_col = random.randint(0, | ||
+ | turi.setPenColor(colors[r_pen_col]) | ||
+ | | ||
+ | r_fill_col = random.randint(0, | ||
+ | turi.setFillColor(colors[r_fill_col]) | ||
+ | | ||
+ | r_side = random.randint(40, | ||
+ | | ||
+ | turi.setPos(x, | ||
+ | turi.startPath() | ||
+ | for i in range(3): | ||
+ | turi.forward(r_side) | ||
+ | turi.left(120) | ||
+ | | ||
+ | turi.fillPath() | ||
+ | |||
+ | |||
+ | def quadrat(x, | ||
+ | r_width = random.randint(10, | ||
+ | turi.setPenWidth(r_width) | ||
+ | | ||
+ | r_pen_col = random.randint(0, | ||
+ | turi.setPenColor(colors[r_pen_col]) | ||
+ | | ||
+ | r_fill_col = random.randint(0, | ||
+ | turi.setFillColor(colors[r_fill_col]) | ||
+ | | ||
+ | r_side = random.randint(20, | ||
+ | | ||
+ | turi.setPos(x, | ||
+ | turi.startPath() | ||
+ | for i in range(4): | ||
+ | turi.forward(r_side) | ||
+ | turi.left(90) | ||
+ | | ||
+ | turi.fillPath() | ||
+ | |||
+ | r_anz_figs = random.randint(30, | ||
+ | |||
+ | for i in range(r_anz_figs): | ||
+ | x_rand = random.randint(-300, | ||
+ | y_rand = random.randint(-200, | ||
+ | |||
+ | r_figur = random.randint(0, | ||
+ | if r_figur == 0: | ||
+ | quadrat(x_rand, | ||
+ | else: | ||
+ | dreieck(x_rand, | ||
+ | </ | ||
+ | |||
+ | ++++ | ||
==== - Aufgaben (Funktionen) ==== | ==== - Aufgaben (Funktionen) ==== | ||
- | === Aufgabe === | + | === Aufgabe |
Definiere eine Funktion mit einem Argument `volume_cube(x)`, | Definiere eine Funktion mit einem Argument `volume_cube(x)`, | ||
Zeile 1545: | Zeile 1613: | ||
Welches Volumen hat ein Würfel mit Seitenlänge 13 cm? | Welches Volumen hat ein Würfel mit Seitenlänge 13 cm? | ||
- | === Aufgabe === | + | ++++Lösung| |
+ | |||
+ | <code python> | ||
+ | def volume_cube(x): | ||
+ | return x**3 | ||
+ | |||
+ | vol = volume_cube(13) | ||
+ | print(vol) | ||
+ | </ | ||
+ | |||
+ | ++++ | ||
+ | |||
+ | === Aufgabe | ||
Das Volumen einer Kugel mit Radius $R$ ist: $$V = \frac{4\pi}{3}\cdot R^3$$ | Das Volumen einer Kugel mit Radius $R$ ist: $$V = \frac{4\pi}{3}\cdot R^3$$ | ||
Definiere eine Funktion `volume_sphere(...)`, | Definiere eine Funktion `volume_sphere(...)`, | ||
- | === Aufgabe === | + | ++++Lösung| |
+ | |||
+ | <code python> | ||
+ | import math | ||
+ | |||
+ | def volume_sphere(r): | ||
+ | return 4 * math.pi / 3 * r**3 | ||
+ | |||
+ | vol = volume_sphere(5) | ||
+ | print(vol) | ||
+ | </ | ||
+ | |||
+ | ++++ | ||
+ | |||
+ | === Aufgabe | ||
Schreibe eine Funktion `grade(points)`, | Schreibe eine Funktion `grade(points)`, | ||
Zeile 1559: | Zeile 1653: | ||
* Erreicht man mehr Punkte als notwendig für Note 6, soll man trotzdem die Note 6 erhalten. | * Erreicht man mehr Punkte als notwendig für Note 6, soll man trotzdem die Note 6 erhalten. | ||
- | === Aufgabe === | + | ++++Lösung| |
+ | |||
+ | <code python> | ||
+ | POINTS_FOR_SIX = 22 | ||
+ | |||
+ | def grade(points): | ||
+ | g = round(5 * points / POINTS_FOR_SIX + 1,2) | ||
+ | if g > 6: | ||
+ | return 6 | ||
+ | return g # kann auch vorher noch else: schreiben, macht keinen Unterschied | ||
+ | |||
+ | print(grade(0)) | ||
+ | print(grade(20)) | ||
+ | print(grade(22)) | ||
+ | print(grade(24)) | ||
+ | </ | ||
+ | |||
+ | ++++ | ||
+ | |||
+ | |||
+ | === Aufgabe | ||
Schreibe eine Funktion `liste_vielfache(vv, | Schreibe eine Funktion `liste_vielfache(vv, | ||
- | === Aufgabe === | + | ++++Lösung| |
+ | |||
+ | <code python> | ||
+ | def liste_vielfache(vv, | ||
+ | li = [] # Wichtig, dass in und NICHT ausserhalb der Funktion definiert wird. Warum? | ||
+ | for i in range(1, | ||
+ | li.append(i*vv) | ||
+ | return li | ||
+ | |||
+ | print(liste_vielfache(3, | ||
+ | </ | ||
+ | |||
+ | ++++ | ||
+ | |||
+ | === Aufgabe | ||
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$. | 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 `faculty(...)`, welcher als Argument eine ganze Zahl übergeben wird und welche dir dann die Fakultät dieser Zahl zurückgibt. | + | Schreibe eine Funktion `factorial(...)`, welcher als Argument eine ganze Zahl übergeben wird und welche dir dann die Fakultät dieser Zahl zurückgibt. |
**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! | **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 (Zusatzaufgabe) === | + | ++++Lösung| |
- | **Mitternachtsformel:** Eine quadratische Funktion kann immer in die Form $$ax^2 + bx + c = 0$$ gebracht werden. Die Lösung ist gegeben durch: | + | <code python> |
+ | def factorial(n): | ||
+ | p = 1 | ||
+ | for i in range(1, | ||
+ | p = p * i | ||
+ | return p | ||
+ | |||
+ | print(factorial(0)) | ||
+ | print(factorial(1)) | ||
+ | print(factorial(2)) | ||
+ | print(factorial(3)) | ||
+ | print(factorial(4)) | ||
+ | print(factorial(5)) | ||
+ | </ | ||
+ | |||
+ | ++++ | ||
+ | |||
+ | === Aufgabe Mitternachtsformel (Zusatzaufgabe) === | ||
+ | |||
+ | Eine quadratische Funktion kann immer in die Form $$ax^2 + bx + c = 0$$ gebracht werden. Die Lösung ist gegeben durch die sogenannte **Mitternachtsformel**: | ||
$$x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}$$ | $$x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}$$ | ||
Zeile 1588: | Zeile 1735: | ||
* $x^2 - 4 x + 4 = 0$ hat eine Lösung: $2$ | * $x^2 - 4 x + 4 = 0$ hat eine Lösung: $2$ | ||
* $x^2 + 2 x + 7 = 0$ hat keine Lösung | * $x^2 + 2 x + 7 = 0$ hat keine Lösung | ||
+ | |||
+ | |||
+ | ++++Lösung| | ||
+ | |||
+ | <code python> | ||
+ | def mitternachtsformel(a, | ||
+ | d = b**2 - 4*a*c | ||
+ | if d < 0: | ||
+ | return [] | ||
+ | elif d == 0: | ||
+ | return [-b/(2*a)] | ||
+ | else: | ||
+ | return [(-b-sqrt(d))/ | ||
+ | | ||
+ | print(mitternachtsformel(3, | ||
+ | print(mitternachtsformel(1, | ||
+ | print(mitternachtsformel(1, | ||
+ | |||
+ | </ | ||
+ | |||
+ | ++++ | ||