Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lehrkraefte:blc:informatik:ffprg1-2020:loops [2020/02/03 17:53] Ivo Blöchliger [break, continue] |
lehrkraefte:blc:informatik:ffprg1-2020:loops [2022/04/07 13:16] (current) Ivo Blöchliger [Aufgaben] |
||
---|---|---|---|
Line 38: | Line 38: | ||
k=100 | k=100 | ||
p=1.01 | p=1.01 | ||
- | while (k< | + | while (k< |
- | k*=p | + | k*=p # Kurzform für k = k*p |
- | n+=1 | + | n+=1 # Kurzform für n = n+1 |
print(n) | print(n) | ||
</ | </ | ||
Was berechnet das obige Programm? | Was berechnet das obige Programm? | ||
+ | <hidden Lösung> | ||
+ | Das Programm berechnet die Anzahl Jahre, die nötig sind, bis das Ausgangskapital $k$ bei einem Zinsatz von $p-1$ auf über 200 angewachsen ist. | ||
+ | </ | ||
==== Endlos-Schleife ==== | ==== Endlos-Schleife ==== | ||
<code python> | <code python> | ||
- | while true: | + | import time |
+ | while True: | ||
| | ||
+ | | ||
</ | </ | ||
===== break, continue ===== | ===== break, continue ===== | ||
- | Manchmal ist es wünschenswert, | + | Manchmal ist es wünschenswert, |
<code python> | <code python> | ||
Line 60: | Line 65: | ||
if a>5: | if a>5: | ||
break # Sofort die while-Schlaufe beenden | break # Sofort die while-Schlaufe beenden | ||
+ | print(" | ||
</ | </ | ||
Line 65: | Line 71: | ||
a = 1 | a = 1 | ||
while a<10: | while a<10: | ||
- | if a%2==0: | + | if a%2==0: |
a+=3 | a+=3 | ||
| | ||
Line 79: | Line 85: | ||
print(" | print(" | ||
</ | </ | ||
+ | |||
+ | |||
+ | ===== Zeilenumbrüche ===== | ||
+ | Sie können eine print-Ausgabe ohne Zeilenumbruch erreichen, indem Sie nach der schliessenden Klammer ein Komma anfügen: | ||
+ | <code python> | ||
+ | print(" | ||
+ | print(" | ||
+ | </ | ||
+ | Leider wird damit auch ein Leerschlag eingefügt. | ||
+ | |||
+ | Noch mehr Kontrolle hat man, wen man die ganze Zeile erst in einem String aufbaut und diesen am Schluss ausgibt: | ||
+ | <code python> | ||
+ | zeile = "Die Zahlen 1" | ||
+ | for i in range(2,5): | ||
+ | zeile += ", %d" % (i*i) | ||
+ | print(zeile) | ||
+ | </ | ||
====== Aufgaben ====== | ====== Aufgaben ====== | ||
Line 88: | Line 111: | ||
Sie können wie folgt überprüfen, | Sie können wie folgt überprüfen, | ||
<code python> | <code python> | ||
- | if a % b == 0: # Wenn der Rest der Division von a durch b gleich Null ist, ist a druch b teilbar | + | if a % b == 0: # Wenn der Rest der Division von a durch b gleich Null ist, ist a durch b teilbar |
# Tu was | # Tu was | ||
</ | </ | ||
- | Sie können eine print-Ausgabe ohne Zeilenumbruch erreichen, indem Sie nach der schliessenden Klammer ein Komma anfügen: | ||
- | <code python> | ||
- | print(" | ||
- | print(" | ||
- | </ | ||
- | Leider wird damit auch ein Leerschlag eingefügt. Eine flexiblere Methode ist die Verwendung von sys.stdout.write(): | ||
- | <code python> | ||
- | import sys | ||
- | |||
- | sys.stdout.write(" | ||
- | sys.stdout.write(" | ||
- | </ | ||
* Schreiben Sie ein Programm, das sämtliche Teiler der eingegebenen Zahl ausgibt. | * Schreiben Sie ein Programm, das sämtliche Teiler der eingegebenen Zahl ausgibt. |