Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.

Link zu der Vergleichsansicht

Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung
Nächste Überarbeitung
Vorherige Überarbeitung
gf_informatik:funktionen [2025-11-20 13:12] – [Funktionen] hofgf_informatik:funktionen [2026-04-07 09:40] (aktuell) – [Aufgabe E5] hof
Zeile 39: Zeile 39:
 Die Funktion gibt einfach "Hallo du!" aus, wenn sie aufgerufen wird. Die ersten beiden Zeilen definieren die Funktion. Unten wird sie zweimal aufgerufen, dementsprechend wird 2x "Hallo du!" ausgegeben. Die Funktion gibt einfach "Hallo du!" aus, wenn sie aufgerufen wird. Die ersten beiden Zeilen definieren die Funktion. Unten wird sie zweimal aufgerufen, dementsprechend wird 2x "Hallo du!" ausgegeben.
  
-<html><script type="module" src="https://bottom.ch/ksr/ed/bottom-editor.js"></script></html>+<html><script type="module" src="https://bottom.ch/editor/stable/bottom-editor.js"></script></html>
 <html><bottom-editor> <html><bottom-editor>
 def say_hi(): def say_hi():
Zeile 56: Zeile 56:
 <html><bottom-editor> <html><bottom-editor>
 def say_hi(name): def say_hi(name):
-    print("Hallo " + name + "!")+    print(f"Hallo {name}!")
  
 say_hi("Silvia") say_hi("Silvia")
Zeile 129: Zeile 129:
 ++++Tipps:| ++++Tipps:|
 Erzeuge mithilfe des `random`-Moduls eine [[gf_informatik:programmieren_ii#zufallszahlen|Zufallszahl]] ($1$ oder $2$). Falls die Zufallszahl $1$ ($2$) ist, gibst du "Zahl" ("Kopf") aus. Verwende dazu eine Verzweigung. Erzeuge mithilfe des `random`-Moduls eine [[gf_informatik:programmieren_ii#zufallszahlen|Zufallszahl]] ($1$ oder $2$). Falls die Zufallszahl $1$ ($2$) ist, gibst du "Zahl" ("Kopf") aus. Verwende dazu eine Verzweigung.
 +
 +
 +Simuliere nun 20 Münzenwürfe.
 +
 ++++ ++++
 </nodisp> </nodisp>
Zeile 142: Zeile 146:
 ++++Tipps:| ++++Tipps:|
 Ähnlich wie Münzwurf-Funktion oben. Bestimme wieder eine Zufallszahl (z.B. im Bereich $1-5$, falls du fünf Sprüche hast). Falls die Zufallszahl $2$ ist, gibst du den zweiten Spruch aus. Verwende dazu eine if-elif-...-else-Verzweigung. Ähnlich wie Münzwurf-Funktion oben. Bestimme wieder eine Zufallszahl (z.B. im Bereich $1-5$, falls du fünf Sprüche hast). Falls die Zufallszahl $2$ ist, gibst du den zweiten Spruch aus. Verwende dazu eine if-elif-...-else-Verzweigung.
- 
-Simuliere nun 20 Münzenwürfe. 
 ++++ ++++
 </nodisp> </nodisp>
Zeile 175: Zeile 177:
    * Du kannst auch weitere Funktionen definieren, die dir das Leben erleichtern.    * Du kannst auch weitere Funktionen definieren, die dir das Leben erleichtern.
  
-<nodisp 2>+<nodisp 1>
 ++++Lösungen Aufgaben E| ++++Lösungen Aufgaben E|
  
Zeile 182: Zeile 184:
 === Aufgabe E1 === === Aufgabe E1 ===
  
-<code python>+<html><bottom-editor>
 def greetings(name,residence): def greetings(name,residence):
-    print("Hallo, mein(e) liebe(r) " + name + " aus " + residence + ". Ich wünsche dir einen ganz tollen Tag!")+    print(f"Hallo, mein(e) liebe(r) {nameaus {residence}. Ich wünsche dir einen ganz tollen Tag!")
  
-greetings("Fritz","Romanshorn"+greetings("Fritz", "Romanshorn"
-greetings("Monika","Amriswil"+greetings("Monika", "Amriswil"
-</code>+</bottom-editor></html>
  
 === Aufgabe E2 === === Aufgabe E2 ===
  
-<code python>+<html><bottom-editor>
 import random import random
  
-""" +Wichtig: Die Ermittlung der Zufallszahl mit randint und die 
-Wichtig: Die Ermittlung der Zufallszahl mit randint und die Verzweigung (Kopf oder Zahl) muss IN der Funktion gemacht werden, nicht ausserhalb! +Verzweigung (Kopf oder Zahl) muss IN der Funktion gemacht 
-"""+werden, nicht ausserhalb!
  
 def head_or_tail(): def head_or_tail():
Zeile 208: Zeile 210:
 head_or_tail() head_or_tail()
 head_or_tail() head_or_tail()
-</code>+</bottom-editor></html>
  
 === Aufgabe E3 === === Aufgabe E3 ===
  
-<code python>+<html><bottom-editor>
 import random import random
  
Zeile 218: Zeile 220:
     r = random.randint(1,3)     r = random.randint(1,3)
     if r == 1:     if r == 1:
-        print("Spruch 1")+        print("Morgenstund ist aller Laster anfang!")
     elif r == 2:     elif r == 2:
-        print("Spruch 2")+        print("Der Apfel fällt nicht weit vom Birnbaum!")
     else:     else:
-        print("Spruch 3") +        print("Wer andern in der Nase bohrt, ist selbst ein Schwein.") 
-</code>+ 
 +fortune_cookie() 
 +</bottom-editor></html>
  
 === Aufgabe E4 === === Aufgabe E4 ===
  
-<code python>+<html><bottom-editor layout="split">
 from turtle import * from turtle import *
 turi = Turtle() turi = Turtle()
Zeile 244: Zeile 248:
 square(150) square(150)
 square(200) square(200)
-</code>+</bottom-editor></html>
  
 === Aufgabe E5 === === Aufgabe E5 ===
  
-<code python>+<html><bottom-editor layout="split">
 from turtle import * from turtle import *
 t = Turtle() t = Turtle()
-t.hideturtle() 
  
 def square(x,y,l): def square(x,y,l):
Zeile 287: Zeile 290:
 rectangle(-150,50,20,100) rectangle(-150,50,20,100)
 triangle(100,150,40) triangle(100,150,40)
-</code>+</bottom-editor></html>
  
-[[https://webtigerpython.ethz.ch/?code=NobwRAdghgtgpmAXGGUCWEB0AHAnmAGjABMoAXKJMNGbAewCcyACBqCYumAHQgDMGXZmQCuTADZxmNek2YAqXiwC8zACpiykgBQBKJZgAWaYnFES4egwGdscOMW0BGAAwv9EXqb7NrARxEoBksADwJcAnFdRF5mOOFMMjhJWTJtMNwPeOlmVRdY-IB3Y0kcgB5mABYYiGzsskw-RyiCuoSGNABzQzSATndWurRcnIBqZideLzgfAGM0BlmdDIIGaMGGpJTGNIys-Ib5xZ01qY4Z1jhZighO5fCCKAIAI3Xag8TkuFT08P244Z5QbFNClYYVABMNTacQaTW0UH-dQaHW6fQG7zacMcr0G9UwqJ62n6SOygLGEzO3mEHXYd1CDyi0I-W2-O1-mUG5PymOYILBzAqAGZmVjGs1SR9CWknBCMTCcqphuNJp5zj42BwuAB9WZ0cSMbRkTW2IJwCBkZQAMSg4mscDe2WCxBGms4MAJ7GIGBlBGYEIArAGkZ1gubXV6uJ6OD7nH7A8HBs9xCIpKo3VG3bGnPGg0i0D5jexTcELaK6rbsIYoBGtR6sxbtAGXLnE7ynWYxLVtM6_aH7BA_cnU37K9Wkcl7eWO-Zu73mP3zUOUw6qRcgmQ0NY0o74tZq3ZrLX3dHvY3KgRXEj-VJ91BD8wAHzMFzTvcHuBH1R3h8AWkp7bvveaasJG9ZetmfqVJKcR0qU6ZgaesYtswJJ4h8kh8GkcGroBcQhMemYQY2v5OAAHCh5HyjCuCEeBMYkVRfpUTBvgmHATh0UhjZysxbisXCoLiHqBoMD2YG6vqhpqAwqa6AJmCFCYZCGNo1Fis8cCdBg2p8EJVh4dIPg_mmqhOG-bT-IEwS_MwERsaYTisckBa-B-uSqFC6FtEcSwMnZfrWOxTnecwLnGe5yiqCKoX1LStz3AFDkcc54iuSZHlVBZdRBaYEJcQ2aS8RM_GxfEwTXDhtn2blHGBexEIKeaxC6fprHZLwG5bjuvBgAAvgAukAA|Modern Artist]]+<html><bottom-editor layout="split"> 
 +import random 
 +from turtle import * 
 + 
 +def square(t,l): 
 +    i = 0 
 +    while i < 4: 
 +        t.fd(l) 
 +        t.right(90) 
 +        i = i + 1 
 + 
 +def circle(t,r): 
 +    t.circle(r) 
 + 
 +def rectangle(t,a,b): 
 +    i = 0 
 +    while i < 2: 
 +        t.fd(a) 
 +        t.right(90) 
 +        t.fd(b) 
 +        t.right(90) 
 +        i = i + 1 
 + 
 +def triangle(t,l): 
 +    i = 0 
 +    while i < 3: 
 +        t.fd(l) 
 +        t.right(120) 
 +        i = i + 1 
 + 
 +def random_color(transparent=False): 
 +    red = random.randint(1, 255) 
 +    green = random.randint(1, 255) 
 +    blue = random.randint(1, 255) 
 +    if transparent: 
 +        alpha = random.randint(50, 255) 
 +        return (red, green, blue, alpha) 
 +    else: 
 +        return (red, green, blue) 
 + 
 +def artist(t): 
 +    shapes = random.randint(4,10) 
 +    while shapes > 0: 
 +        shapes shapes 
 +        shape = random.randint(1, 4) 
 +        angle = random.randint(0, 90) 
 +        t.left(angle) 
 +        x = random.randint(-180, 180) 
 +        y = random.randint(-180, 180) 
 +        t.teleport(x, y) 
 +        side1 = random.randint(20, 100) 
 +        t.fillcolor(random_color(True)) 
 +        t.width(0) 
 +        t.begin_fill() 
 +        if shape == 1: 
 +            square(t, side1) 
 +        elif shape == 2: 
 +            circle(t, side1) 
 +        elif shape == 3: 
 +            triangle(t, side1) 
 +        elif shape == 4: 
 +            side2 = random.randint(20, 100) 
 +            rectangle(t, side1, side2) 
 +        t.end_fill() 
 + 
 +t = Turtle() 
 +t.hideturtle() 
 +t.speed(100) 
 + 
 +artist(t) 
 +</bottom-editor></html>
  
 ++++ ++++
Zeile 350: Zeile 423:
    * keine Lösung: gib `None` zurück, dies ist der Fall, wenn der Term in der Wurzel negativ ist    * 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    * eine Lösung, dies ist der Fall, wenn der Term in der Wurzel genau 0 ist
-   * zwei Lösungen: gib Liste mit den beiden Werten zurück+   * zwei Lösungen: gib beide Werte (mit Komma getrennt) zurück
  
 Tipp: Verwende die Diskriminante, um den richtigen Fall zu ermitteln. Tipp: Verwende die Diskriminante, um den richtigen Fall zu ermitteln.
Zeile 360: Zeile 433:
    * $x^2 + 2 x + 7 = 0$ hat keine Lösung    * $x^2 + 2 x + 7 = 0$ hat keine Lösung
  
-<nodisp 2>+<nodisp 1>
  
 ++++Lösungen Aufgaben F| ++++Lösungen Aufgaben F|
Zeile 368: Zeile 441:
 === Aufgabe F1 === === Aufgabe F1 ===
  
-<code python>+<html><bottom-editor>
 def volume_cube(x): def volume_cube(x):
     return x**3     return x**3
Zeile 374: Zeile 447:
 v = volume_cube(13) v = volume_cube(13)
 print(v) print(v)
-</code>+</bottom-editor></html>
  
 === Aufgabe F2 === === Aufgabe F2 ===
  
-<code python>+<html><bottom-editor> 
 +import math
 def pythagoras(a,b): def pythagoras(a,b):
-    return sqrt(a*a + b*b)+    return math.sqrt(a*a + b*b)
  
 print(pythagoras(3,4)) print(pythagoras(3,4))
-</code>+</bottom-editor></html>
  
  
 === Aufgabe F3 === === Aufgabe F3 ===
  
-<code python>+<html><bottom-editor>
 import math import math
 def volume_sphere(r): def volume_sphere(r):
Zeile 394: Zeile 468:
  
 print(volume_sphere(3)) print(volume_sphere(3))
-</code>+</bottom-editor></html>
  
  
 === Aufgabe F4 === === Aufgabe F4 ===
  
-<code python>+<html><bottom-editor>
 def grade(points,points_6): def grade(points,points_6):
     gr = 5*points/points_6 + 1     gr = 5*points/points_6 + 1
Zeile 407: Zeile 481:
  
 print(grade(23,51)) print(grade(23,51))
-</code>+</bottom-editor></html>
  
  
 === Aufgabe F5 === === Aufgabe F5 ===
  
-<code python>+<html><bottom-editor>
 def factorial(n): def factorial(n):
     product = 1     product = 1
Zeile 422: Zeile 496:
  
 print(factorial(5)) print(factorial(5))
-</code>+</bottom-editor></html>
  
  
 === Aufgabe F6 === === Aufgabe F6 ===
  
-<code python>+<html><bottom-editor> 
 +from math import *
 def mitternachtsformel(a,b,c): def mitternachtsformel(a,b,c):
     d = b*b - 4*a*c     d = b*b - 4*a*c
Zeile 436: Zeile 511:
     else:     else:
         r = -b / (2*a)         r = -b / (2*a)
-        return [(-b - sqrt(d)) / (2*a), (-b + sqrt(d)) / (2*a)]+        return (-b - sqrt(d)) / (2*a), (-b + sqrt(d)) / (2*a)
  
 print(mitternachtsformel(3,-6,-5)) # 2 Loesungen print(mitternachtsformel(3,-6,-5)) # 2 Loesungen
 print(mitternachtsformel(1,-4,4))  # 1 Loesung print(mitternachtsformel(1,-4,4))  # 1 Loesung
 print(mitternachtsformel(1,2,7))   # keine Loesung print(mitternachtsformel(1,2,7))   # keine Loesung
-</code>+</bottom-editor></html>
  
 ++++ ++++
  
 </nodisp> </nodisp>
  • gf_informatik/funktionen.1763644372.txt.gz
  • Zuletzt geändert: 2025-11-20 13:12
  • von hof