while-Schleife

Python für Fortgeschrittene

Hugo Bowne-Anderson

Data Scientist at DataCamp

if-elif-else

control.py

  • Läuft nur einmal durch!
z = 6
if z % 2 == 0 :  # True
    print("z is divisible by 2")  # Executed
elif z % 3 == 0 :
    print("z is divisible by 3")
else :
    print("z is neither divisible by 2 nor by 3")

...   # Moving on
  • While-Schleife = wiederholte if-Anweisung
Python für Fortgeschrittene

While

while condition :
    expression
  • Numerisches Berechnung
  • „Aktion wiederholen, bis die Bedingung erfüllt ist.“
  • Beispiel
    • Der Fehlerwert startet bei 50.
    • Teile den Fehlerwert bei jedem Durchlauf durch Vier.
    • Mache weiter, bis der Fehler nicht mehr größer als Eins ist.
Python für Fortgeschrittene

While

while condition :
    expression

while_loop.py

error = 50.0


while error > 1:
error = error / 4
print(error)
  • Der Fehlerwert startet bei 50.
  • Teile den Fehlerwert bei jedem Durchlauf durch Vier.
  • Mache weiter, bis der Fehler nicht mehr größer als Eins ist.
Python für Fortgeschrittene

While

while condition :
    expression

while_loop.py

error = 50.0
#     50
while error > 1:    # True
      error = error / 4
      print(error)
12.5
Python für Fortgeschrittene

While

while condition :
    expression

while_loop.py

error = 50.0
#     12.5
while error > 1:    # True
      error = error / 4
      print(error)
12.5
3.125
Python für Fortgeschrittene

While

while condition :
    expression

while_loop.py

error = 50.0
#     3.125
while error > 1:    # True
      error = error / 4
      print(error)
12.5
3.125
0.78125
Python für Fortgeschrittene

While

while condition :
    expression

while_loop.py

error = 50.0
#     0.78125
while error > 1:    # False
    error = error / 4
    print(error)
12.5
3.125
0.78125
Python für Fortgeschrittene

While

while condition :
    expression

while_loop.py

error = 50.0
while error > 1 :    # always True
    # error = error / 4
    print(error)
50
50
50
50
50
50
50
...
  • DataCamp: Sitzung unterbrochen
  • Lokales System: Strg + C
Python für Fortgeschrittene

Lass uns üben!

Python für Fortgeschrittene

Preparing Video For Download...