8.1. Loop While About

8.1.1. Rationale

>>> data = ['a', 'b', 'c']
>>>
>>> data[0]
'a'
>>> data[1]
'b'
>>> data[2]
'c'
>>> data = ['a', 'b', 'c']
>>> i = 0
>>>
>>> if i < 3:
...     print(data[i])
...     i += 1
a
>>>
>>> if i < 3:
...     print(data[i])
...     i += 1
b
>>>
>>> if i < 3:
...     print(data[i])
...     i += 1
c
>>> data = ['a', 'b', 'c']
>>> i = 0
>>>
>>> while i < 3:
...     print(data[i])
...     i += 1
a
b
c

8.1.2. Syntax

  • Continue execution when argument is True

  • Stops if argument is False

while loop generic syntax:

while <condition>:
    <do something>
>>> 
... while True:
...     pass

8.1.3. Infinite Loop

Never ending loop. Used in servers to wait forever for incoming connections. Used in games for game logic.

>>> 
... while True:
...     print('hello')

8.1.4. Until

Has stop conditions

>>> i = 0
>>>
>>> while i < 3:
...     print(i)
...     i += 1
0
1
2

8.1.5. Sequence Iteration

Better idea for this is to use for loop. for loop supports Iterators. len() must write all numbers to memory, to calculate its length:

>>> i = 0
>>> data = ['a', 'b', 'c']
>>>
>>> while i < 3:
...     print(i, data[i])
...     i += 1
0 a
1 b
2 c
>>> i = 0
>>> data = ['a', 'b', 'c']
>>> length = 3
>>>
>>> while i < length:
...     print(i, data[i])
...     i += 1
0 a
1 b
2 c
>>> i = 0
>>> data = ['a', 'b', 'c']
>>>
>>> while i < len(data):
...     print(i, data[i])
...     i += 1
0 a
1 b
2 c

8.1.6. Good Practices

  • The longer the loop scope, the longer the variable name should be

  • Avoid one letters if scope is longer than one line

  • Generic names:

    • i - for loop counter

    • abort - for abort flags

    • abort_flag - for abort flags

8.1.7. Assignments

Code 8.1. Solution
"""
* Assignment: Loop While to Float
* Required: yes
* Complexity: easy
* Lines of code: 5 lines
* Time: 5 min

English:
    1. Create `result: list[float]`
    2. Use `while` to iterate over `DATA`
    3. Convert current elements of `DATA` to `float`
    4. Converted value append to `result`
    5. Run doctests - all must succeed

Polish:
    1. Stwórz `result: list[float]`
    2. Użyj `while` do iterowania po `DATA`
    3. Przekonwertuj obecny element `DATA` do `float`
    4. Przekonwertowaną wartość dodaj na koniec `result`
    5. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `Stop` or `Ctrl+C` kills infinite loop

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> type(result)
    <class 'list'>

    >>> assert all(type(x) is float for x in result)

    >>> result
    [2.0, 3.0, 3.5, 4.0, 4.5, 5.0]
"""

DATA = (2, 3, 3.5, 4, 4.5, 5)

# list[float]: values from DATA converted to float
result = ...

Code 8.2. Solution
"""
* Assignment: Loop While to Str
* Required: yes
* Complexity: easy
* Lines of code: 4 lines
* Time: 5 min

English:
    1. Create `result: str`
    2. Use `while` to iterate over `DATA`
    3. Add current element of `DATA` to `result`
    4. Do not use `str.join()`
    5. Run doctests - all must succeed

Polish:
    1. Stwórz `result: str`
    2. Użyj `while` do iterowania po `DATA`
    3. Dodaj obecny element z `DATA` do `result`
    4. Nie używaj `str.join()`
    5. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `Stop` or `Ctrl+C` kills infinite loop

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> type(result)
    <class 'str'>
    >>> result
    'hello'
"""

DATA = ['h', 'e', 'l', 'l', 'o']

# str: joined DATA values
result = ...

Code 8.3. Solution
"""
* Assignment: Loop While Translate
* Required: yes
* Complexity: medium
* Lines of code: 5 lines
* Time: 5 min

English:
    1. Use `while` to iterate over `DATA`
    2. If letter is in `PL` then use conversion value as letter
    3. Add letter to `result`
    4. Run doctests - all must succeed

Polish:
    1. Użyj `while` do iteracji po `DATA`
    2. Jeżeli litera jest w `PL` to użyj skonwertowanej wartości jako litera
    3. Dodaj literę do `result`
    4. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `Stop` or `Ctrl+C` kills infinite loop

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> type(result)
    <class 'str'>
    >>> result
    'zazolc gesla jazn'
"""

PL = {'ą': 'a', 'ć': 'c', 'ę': 'e',
      'ł': 'l', 'ń': 'n', 'ó': 'o',
      'ś': 's', 'ż': 'z', 'ź': 'z'}

DATA = 'zażółć gęślą jaźń'

# str: DATA with substituted PL diacritic chars to ASCII letters
result = ...