Cyklus while

Intermediate Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

if-elif-else

control.py

  • Konstrukcí projde jen jednou!
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
  • Cyklus while = opakovaný příkaz if
Intermediate Python

While

while condition :
    expression
  • Numerický výpočetní model
  • "opakování akce, dokud není splněna podmínka"
  • Příklad
    • Chyba začíná na 50
    • V každém kroku dělte chybu 4
    • Pokračujte, dokud chyba není > 1
Intermediate Python

While

while condition :
    expression

while_loop.py

error = 50.0


while error > 1:
error = error / 4
print(error)
  • Chyba začíná na 50
  • V každém kroku dělte chybu 4
  • Pokračujte, dokud chyba není > 1
Intermediate Python

While

while condition :
    expression

while_loop.py

error = 50.0
#     50
while error > 1:    # True
      error = error / 4
      print(error)
12.5
Intermediate Python

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
Intermediate Python

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
Intermediate Python

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
Intermediate Python

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: relace odpojena
  • Lokální systém: Control + C
Intermediate Python

Pojďme cvičit!

Intermediate Python

Preparing Video For Download...