# Lussen

# While

while «condition»:






 












# ./python/syntax/loops/while.py
iteration = 0
stop = 5
is_true = True

while is_true:
    print('Doing this because it’s still True.',
          f'iteration == {iteration}',
          f'is_true == {is_true}',
          )
    iteration += 1
    if iteration >= stop:
        is_true = False

print(f'iteration == {iteration}',
      f'is_true == {is_true}',
      )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ python3 ./python/syntax/loops/while.py
Doing this because it’s still True. iteration == 0 is_true == True
Doing this because it’s still True. iteration == 1 is_true == True
Doing this because it’s still True. iteration == 2 is_true == True
Doing this because it’s still True. iteration == 3 is_true == True
Doing this because it’s still True. iteration == 4 is_true == True
iteration == 5 is_true == False
$ _

Opgelet

De Do-While-lus bestaat niet in Python. Als alternatief kan je de Game Loop gebruiken.

# For

for «item» in «sequence»:


 


# ./python/syntax/loops/for__1.py
for character in 'Lorem':
    print(character)
1
2
3
$ python3 ./python/syntax/loops/for__1.py
L
o
r
e
m
$ _



 


# ./python/syntax/loops/for__2.py
stop = 5

for i in range(stop):
    print(f'Counting to {stop}: {i}')
1
2
3
4
5
$ python3 ./python/syntax/loops/for__2.py
Counting to 5: 0.
Counting to 5: 1.
Counting to 5: 2.
Counting to 5: 3.
Counting to 5: 4.
$ _




 


# ./python/syntax/loops/for__3.py
start = 1
stop = 5

for i in range(start, stop + 1):
    print(f'Counting to {stop}: {i}')
1
2
3
4
5
6
$ python3 ./python/syntax/loops/for__3.py
Counting to 5: 1.
Counting to 5: 2.
Counting to 5: 3.
Counting to 5: 4.
Counting to 5: 5.
$ _





 


# ./python/syntax/loops/for__4.py
start = 0
stop = 100
step = 20

for i in range(start, stop + 1, step):
    print(f'Counting to {stop}: {str(i).rjust(3)}')
1
2
3
4
5
6
7
$ python3 ./python/syntax/loops/for__4.py
Counting to 100:   0.
Counting to 100:  20.
Counting to 100:  40.
Counting to 100:  60.
Counting to 100:  80.
Counting to 100: 100.
$ _

# Count-Value

De functie enumerate() (opens new window) geeft een iterator van de klasse enumerate terug die telken een tuple met daarin een teller (count) en het item (value) teruggeeft.







 


# ./python/syntax/loops/for__5.py
start = 0
stop = 100
step = 20
r = range(start, stop + 1, step)

for count, value in enumerate(r):
    print(f'{count}. Counting to {stop}: {str(value).rjust(3)}')
1
2
3
4
5
6
7
8
$ python3 ./python/syntax/loops/for__4.py
0. Counting to 100:   0.
1. Counting to 100:  20.
2. Counting to 100:  40.
3. Counting to 100:  60.
4. Counting to 100:  80.
5. Counting to 100: 100.
$ _

# Lussen onderbreken

# Continue

Met continue stop je een enkele iteratie (herhaling).









 


# ./python/syntax/loops/continue.py
start = 1
last = 5
stop = last + 1

for i in range(start, stop):
    if i % 2 == 0:
        print('Skipping the rest of this iteration.')
        continue
    print(f'Counting to {last}: {i}.')
1
2
3
4
5
6
7
8
9
10
$ python3 ./python/syntax/loops/continue.py
Counting to 5: 1.
Skipping the rest of this iteration.
Counting to 5: 3.
Skipping the rest of this iteration.
Counting to 5: 5.
$ _

# Break

Met break stop je de lus.









 


# ./python/syntax/loops/break.py
start = 1
last = 5
stop = last + 1

for i in range(start, stop):
    if i % 2 == 0:
        print('Skipping the rest of this loop.')
        break
    print(f'Counting to {last}: {i}.')
1
2
3
4
5
6
7
8
9
10
$ python3 ./python/syntax/loops/break.py
Counting to 5: 1.
Skipping the rest of this loop.
$ _

# De Game Loop


 



 



# ./python/syntax/loops/game_loop.py
while True:
    answer = input('Give answer: ')
    if 0 < len(answer):
        print(f'You’re answer is: {answer}')
        break

print('Game over')
1
2
3
4
5
6
7
8
$ python3 ./python/syntax/loops/game_loop.py
Give answer: 
Give answer:
Give answer: hello
You’re answer is: hello
Game over
$ _
© 2024 Arteveldehogeschool Laatst bijgewerkt: 14/2/2020, 11:07:52