Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
lehrkraefte:blc:informatik:glf20:programmieren:while [2020/10/28 07:55] mirco.triner |
lehrkraefte:blc:informatik:glf20:programmieren:while [2020/10/30 13:19] mirco.triner |
||
---|---|---|---|
Line 18: | Line 18: | ||
print(counter) | print(counter) | ||
counter += 1 | counter += 1 | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | Die folgenden Codeblöcke führen zum gleichen Ergebnis: | ||
+ | < | ||
+ | counter += 1 | ||
+ | </ | ||
+ | < | ||
+ | counter = counter + 1 | ||
</ | </ | ||
</ | </ | ||
Line 26: | Line 36: | ||
<WRAP todo> | <WRAP todo> | ||
- | True oder False? Bei den letzen beiden Aufgaben **nicht rechnen**, sondern überlegen! | + | Schreiben Sie nun ein Programm, welches einen Bankautomat simuliert. Der Bankautomat versucht die Anzahl von Noten möglichst gering zu halten. Möchte der Kunde 250.-, dann wird der Automat 1x 200.- und 1x 50.- ausgeben. |
- | <quizlib id=" | + | |
- | <question title="& | + | * TIPP: Benutzen Sie <code>//</code> (ganzzahlige Division) um herauszufinden, |
- | | + | |
- | < | + | |
- | | + | |
- | < | + | |
- | </ | + | |
</ | </ | ||
<WRAP info> | <WRAP info> | ||
- | Mit '' | + | Zufallszahl - Python |
- | < | + | < |
- | a = 4 | + | from random import * |
- | if a<3: | + | x = randint(1, 100) |
- | print("a ist kleiner als 3") | + | print x |
- | else: | + | |
- | print(" | + | |
- | </ | + | |
- | Es ist auch möglich, mit '' | + | |
- | <code python> | + | |
- | name = " | + | |
- | if name==" | + | |
- | print(" | + | |
- | elif name==" | + | |
- | | + | |
- | else: | + | |
- | print(" | + | |
</ | </ | ||
+ | Der oben aufgeführte Code gibt eine ganze Zahl zwischen 1 und 100 aus. | ||
</ | </ | ||
- | |||
<WRAP todo> | <WRAP todo> | ||
- | Schreiben Sie ein Programm, | + | Schreiben Sie ein Programm, |
Beispiel einer Ausgabe | Beispiel einer Ausgabe | ||
<code text> | <code text> | ||
- | -8°C ist kalt. | + | Neue Zahl: 15 |
+ | Zahl zu gross | ||
+ | Neue Zahl: | ||
</ | </ | ||
</ | </ | ||
Line 69: | Line 64: | ||
<WRAP todo> | <WRAP todo> | ||
- | Schreiben | + | Nun drehen wir den Spiess um. Schreiben |
</ | </ | ||
- | <hidden Aufgabe, wenn die Lösung der quadratischen Gleichung schon behandelt wurde> | ||
- | <WRAP todo> | ||
- | Schreiben Sie ein Programm, das die quadratische Gleichung $ax^2+bx+c=0$ löst. Die Koeffizienten $a$, $b$ und $c$ sollen am Anfang direkt im Programmcode definiert werden. | ||
- | Unterscheiden Sie dazu die unterschiedlichen Fälle, die auftreten können und melden Sie die Anzahl der Lösungen und die Lösungen selbst. | + | ===== Lösungsvorschläge ===== |
- | + | <hidden Lösungvorschläge> | |
- | Starten Sie Ihr Programm wie folgt: | + | |
<code python> | <code python> | ||
- | # Definition der Koeffizienten in einer Zeile, durch Kommas getrennt. | + | #Bankautomat |
- | a,b,c = 4,2,-3 | + | |
- | print(" | + | |
- | </ | + | |
- | Beispiel-Ausgaben: | + | |
- | <code text> | + | |
- | Gleichung 4.00x^2+2.00x-3.00 = 0 | + | |
- | Zwei Lösungen x1=-1.151388 und x2=0.651388 | + | |
- | Gleichung 1.00x^2+2.00x+1.00 = 0 | + | print(" |
- | Genau eine Lösung x=-1.000000 | + | cash = inputInt(" |
- | Gleichung 1.00x^2+2.00x+3.00 | + | while cash > 0: |
- | Keine reelle Lösung | + | if cash//1000 > 0: |
- | + | print(" | |
- | Gleichung | + | cash = cash%1000 |
- | Lineare Gleichung, eine Lösung: x=-2.000000 | + | elif cash//200 > 0: |
- | + | | |
- | Gleichung | + | cash = cash%200 |
- | Falsche Aussage, keine Lösung | + | elif cash//100 > 0: |
+ | print(" | ||
+ | cash = cash%100 | ||
+ | elif cash//50 > 0: | ||
+ | print(" | ||
+ | cash = cash%50 | ||
+ | elif cash//20 > 0: | ||
+ | print(" | ||
+ | cash = cash%20 | ||
+ | elif cash//10 > 0: | ||
+ | print(" | ||
+ | cash = cash%10 | ||
+ | | ||
+ | print(" | ||
+ | cash = 0 | ||
- | Gleichung 0.00x^2+0.00x+0.00 = 0 | ||
- | Wahre Aussage, L=R, jedes x ist Lösung | ||
</ | </ | ||
- | </ | ||
- | </ | ||
- | <WRAP info> | ||
- | Der **Modulo-Operator** '' | ||
- | |||
- | Werden Zahlen mit '' | ||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | </ | ||
- | |||
- | <WRAP todo> | ||
- | **FIZZ BUZZ!** Programmieren Sie ein Programm, das alle Zahlen von 1 bis 100 ausgibt, aber alle jene, die durch 3 teilbar sind durch '' | ||
- | <hidden Verlangte Ausgabe bis 30> | ||
- | <code text> | ||
- | 1 | ||
- | 2 | ||
- | fizz | ||
- | 4 | ||
- | buzz | ||
- | fizz | ||
- | 7 | ||
- | 8 | ||
- | fizz | ||
- | buzz | ||
- | 11 | ||
- | fizz | ||
- | 13 | ||
- | 14 | ||
- | fizz buzz | ||
- | 16 | ||
- | 17 | ||
- | fizz | ||
- | 19 | ||
- | buzz | ||
- | fizz | ||
- | 22 | ||
- | 23 | ||
- | fizz | ||
- | buzz | ||
- | 26 | ||
- | fizz | ||
- | 28 | ||
- | 29 | ||
- | fizz buzz | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | ===== Optionale, weitere Aufgaben ===== | ||
- | * Geben Sie alle Teiler einer Zahl aus. | ||
- | * Geben Sie die Primfaktorzerlegung einer Zahl aus. | ||
- | * Bestimmen Sie, ob eine gegebene Zahl eine Primazahl ist oder nicht. | ||
- | * Geben Sie die Primzahlen der Reihe nach aufsteigend aus. | ||
- | |||
- | ===== Lösungsvorschläge ===== | ||
- | <hidden Lösungvorschläge> | ||
<code python> | <code python> | ||
- | for i in range(1,101): | + | from random import * |
- | if i % 3 == 0 | + | to_be_guessed = randint(1, 100) |
- | print("fizz buzz") | + | guess = 0 |
- | | + | while guess != to_be_guessed: |
- | print("fizz") | + | guess = input("Neue Zahl: ") |
- | elif i % 5 == 0: | + | |
- | print("buzz") | + | |
+ | | ||
+ | elif guess < to_be_guessed: | ||
+ | print "Zahl zu klein" | ||
else: | else: | ||
- | print(i) | + | print "Schade, Sie geben also auf!" |
- | </ | + | break |
- | + | ||
- | <code python> | + | |
- | t = inputInt("Temperatur in Grad Celsius") | + | |
- | text = "%d°C ist " % t | + | |
- | if (t< | + | |
- | text += " | + | |
- | elif t<25: | + | |
- | text += " | + | |
else: | else: | ||
- | | + | print "Glückwunsch! Das war's!" |
- | + | ||
- | print(text+".") | + | |
</ | </ | ||
<code python> | <code python> | ||
- | # Definition der Koeffizienten | + | #Zahl zwischen 1-100 |
- | a,b,c = 0,0,0 | + | to_be_guessed |
- | print("Gleichung %.2fx^2%+.2fx%+.2f = 0" | + | guess = 50 |
+ | counter = 1 | ||
- | if a==0: # Lineare Gleichung | + | while guess != to_be_guessed: |
- | if b==0: # Gleichung ohne x | + | if(guess > to_be_guessed): |
- | | + | |
- | print(" | + | |
- | else: | + | |
- | print(" | + | |
else: | else: | ||
- | | + | |
- | else: # Quadratische Gleichung | + | guess += 1 |
- | d = b*b-4*a*c | + | else: |
- | if (d<0): | + | guess += ((to_be_guessed |
- | | + | print(" |
- | elif d==0: | + | counter += 1 |
- | | + | |
- | | + | print(to_be_guessed) |
- | d = d**0.5 # Wurzel aus der Diskriminante | + | |
- | x1 = (-b-d)/ | + | |
- | x2 = (-b+d)/(2*a) | + | |
- | print(" | + | |
</ | </ | ||
</ | </ |