lehrkraefte:blc:informatik:glf20:programmieren:while

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
lehrkraefte:blc:informatik:glf20:programmieren:while [2020/10/28 11:39]
mirco.triner
lehrkraefte:blc:informatik:glf20:programmieren:while [2020/10/30 13:21] (current)
mirco.triner
Line 22: Line 22:
  
 <WRAP info> <WRAP info>
-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.+Die folgenden Codeblöcke führen zum gleichen Ergebnis: 
 +<code> 
 +counter += 1 
 +</code> 
 +<code> 
 +counter = counter + 1 
 +</code> 
 +</WRAP> 
 + 
 +<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. Speichern Sie deshalb Ihr Programm, bevor Sie es ausführen.
 </WRAP> </WRAP>
  
Line 38: Line 48:
 print x print x
 </code> </code>
-Der oben aufgeführte Code gibt eine ganze Zahl zwischen und 100 aus.+Der oben aufgeführte Code gibt eine ganze Zahl zwischen und 100 aus.
 </WRAP> </WRAP>
  
Line 61: Line 71:
 <hidden Lösungvorschläge> <hidden Lösungvorschläge>
 <code python> <code python>
 +#Bankautomat
 +
 print("Guten Tag") print("Guten Tag")
-= inputInt("Welcher Betrag soll ausbezahlt werden?")+cash = inputInt("Welcher Betrag soll ausbezahlt werden?")
  
-while > 0: +while cash > 0: 
-    if t//1000 > 0: +    if cash//1000 > 0: 
-        print(str(t//1000) + "x 1000.-"+        print("%d x 1000.-" % (cash//1000)
-        t%1000 +        cash cash%1000 
-    elif t//200 > 0: +    elif cash//200 > 0: 
-        print(str(t//200) + "x 200.-"+        print("%d x 200.-" % (cash//200)
-        t%200 +        cash cash%200 
-    elif t//100 > 0: +    elif cash//100 > 0: 
-        print(str(t//100) + "x 100.-"+        print("%d x 100.-" % (cash//100)
-        t%100 +        cash cash%100 
-    elif t//50 > 0: +    elif cash//50 > 0: 
-        print(str(t//50) + "x 50.-"+        print("%d x 50.-" % (cash//50)
-        t%50 +        cash cash%50 
-    elif t//20 > 0: +    elif cash//20 > 0: 
-        print(str(t//20) "20.-") +        print("%d x 20.-" % (cash//20)
-        t%20+        cash = cash%20 
 +    elif cash//10 > 0: 
 +        print("%d 10.-" % (cash//10)
 +        cash cash%10
     else:     else:
-        print(str(t//10) + "x 10.-") +        print("und noch ein wenig Münz") 
-        t%10+        cash 
 </code> </code>
  
 <code python> <code python>
-inputInt("Temperatur in Grad Celsius"+from random import * 
-text   "%d°C ist " % t +to_be_guessed randint(1, 100
-if (t<15)    +guess 0 
-    text += "kalt  # eine Abkürzung für text = text + "kalt+while guess != to_be_guessed
-elif t<25+    guess input("Neue Zahl: "
-    text += "warm"+    if guess > 0: 
 +        if guess > to_be_guessed: 
 +            print "Zahl zu gross
 +        elif guess to_be_guessed: 
 +            print "Zahl zu klein" 
 +    else: 
 +        print "Schade, Sie geben also auf!" 
 +        break
 else: else:
-    text += "heiss" +    print "Glückwunsch! Das war's!"
- +
-print(text+".")+
 </code> </code>
  
 <code python> <code python>
-Definition der Koeffizienten +#Zahl zwischen 1-100 
-a,b,c 0,0,0 +to_be_guessed input("Neue Zahl: ") 
-print("Gleichung %.2fx^2%+.2fx%+.2f = 0% (a,b,c))+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)
-        if c==0: +        guess //2
-            print("Wahre Aussage, L=R, jedes x ist Lösung"+
-        else: +
-            print("Falsche Aussage, keine Lösung")+
     else:     else:
-        print("Lineare Gleichung, eine Lösung: x=%f" % (-c/b)+        if((to_be_guessed guess% 2 == 1): 
-else: # Quadratische Gleichung +            guess +1 
-    d b*b-4*a*c  # Diskriminante +        else: 
-    if (d<0): +            guess += ((to_be_guessed guess) /2) 
-        print("Keine reelle Lösung"+    print("Round: %d, Guess %d" % (counterguess)) 
-    elif d==0: +    counter += 1 
-        print("Genau eine Lösung x=%f" % (-b/(2*a))) + 
-    else: +print(to_be_guessed) 
-        d = d**0.5 # Wurzel aus der Diskriminante +   
-        x1 = (-b-d)/(2*a) +
-        x2 = (-b+d)/(2*a+
-        print("Zwei Lösungen x1=%f und x2=%f" % (x1x2))+
 </code> </code>
 </hidden> </hidden>
  • lehrkraefte/blc/informatik/glf20/programmieren/while.1603881549.txt.gz
  • Last modified: 2020/10/28 11:39
  • by mirco.triner