Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
lehrkraefte:blc:informatik:glf20:programmieren:while [2020/10/28 11:39] mirco.triner |
lehrkraefte:blc:informatik:glf20:programmieren:while [2020/10/30 13:20] mirco.triner |
||
---|---|---|---|
Line 22: | Line 22: | ||
<WRAP info> | <WRAP info> | ||
+ | Die folgenden Codeblöcke führen zum gleichen Ergebnis: | ||
+ | < | ||
+ | counter += 1 | ||
+ | </ | ||
+ | < | ||
+ | counter = counter + 1 | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <WRAP alert> | ||
Bei einer While-Schleife kann es leicht passieren, dass die Schleife endlos weiterläuft. Im Gegensatz zu einer For-Schleife muss die Variable bei einer While-Schleife manuell verändert werden. | Bei einer While-Schleife kann es leicht passieren, dass die Schleife endlos weiterläuft. Im Gegensatz zu einer For-Schleife muss die Variable bei einer While-Schleife manuell verändert werden. | ||
</ | </ | ||
Line 38: | Line 48: | ||
print x | print x | ||
</ | </ | ||
- | Der oben aufgeführte Code gibt eine ganze Zahl zwischen | + | Der oben aufgeführte Code gibt eine ganze Zahl zwischen |
</ | </ | ||
Line 61: | Line 71: | ||
<hidden Lösungvorschläge> | <hidden Lösungvorschläge> | ||
<code python> | <code python> | ||
+ | # | ||
+ | |||
print(" | print(" | ||
- | t = inputInt(" | + | cash = inputInt(" |
- | while t > 0: | + | while cash > 0: |
- | if t//1000 > 0: | + | if cash//1000 > 0: |
- | print(str(t// | + | print(" |
- | | + | |
- | elif t//200 > 0: | + | elif cash//200 > 0: |
- | print(str(t//200) + "x 200.-" | + | print(" |
- | | + | |
- | elif t//100 > 0: | + | elif cash//100 > 0: |
- | print(str(t//100) + "x 100.-" | + | print(" |
- | | + | |
- | elif t//50 > 0: | + | elif cash//50 > 0: |
- | print(str(t//50) + "x 50.-" | + | print(" |
- | | + | |
- | elif t//20 > 0: | + | elif cash//20 > 0: |
- | print(str(t//20) + " | + | print("%d x 20.-" % (cash//20)) |
- | | + | cash = cash%20 |
+ | elif cash//10 > 0: | ||
+ | print("%d x 10.-" | ||
+ | | ||
else: | else: | ||
- | print(str(t//10) + "x 10.-") | + | print(" |
- | | + | |
</ | </ | ||
<code python> | <code python> | ||
- | t = inputInt(" | + | from random import * |
- | text | + | to_be_guessed |
- | if (t<15): | + | guess = 0 |
- | | + | while guess != to_be_guessed: |
- | elif t<25: | + | |
- | | + | if guess > 0: |
+ | if guess > to_be_guessed: | ||
+ | print "Zahl zu gross" | ||
+ | elif guess < to_be_guessed: | ||
+ | print "Zahl zu klein" | ||
+ | | ||
+ | print "Schade, Sie geben also auf!" | ||
+ | break | ||
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(" | + | |
</ | </ | ||
</ | </ |