6.2. Block Else

6.2.1. Rationale

  • Unconditional Alternative

  • Optional

  • Executed when condition is not met

6.2.2. Syntax

>>> 
... if <condition>:
...     <do something>
... else:
...     <do something>

6.2.3. Oneline Block

>>> if True:
...     print('True statement')
... else:
...     print('Else statement')
True statement

6.2.4. Multiline Block

>>> if True:
...     print('True statement, first line')
...     print('True statement, second line')
... else:
...     print('Else statement, first line')
...     print('Else statement, second line')
True statement, first line
True statement, second line

6.2.5. Nested Blocks

>>> if True:
...     print('Outer block, true statement, first line')
...     print('Outer block, true statement, second line')
...
...     if True:
...         print('Inner block, true statement, first line')
...         print('Inner block, true statement, second line')
...     else:
...         print('Inner block, else statement, fist line')
...         print('Inner block, else statement, second line')
...
... else:
...     print('Outer block, else statement, first line')
...     print('Outer block, else statement, second line')
Outer block, true statement, first line
Outer block, true statement, second line
Inner block, true statement, first line
Inner block, true statement, second line

6.2.6. Value Check

>>> number = 3
>>>
>>> if number % 2 == 0:
...     print('even')
... else:
...     print('odd')
odd

6.2.7. Conditional Assignment

>>> number = 3
>>>
>>> if number % 2 == 0:
...     status = 'even'
... else:
...     status = 'odd'
>>>
>>> print(status)
odd

6.2.8. Checking If Empty

>>> name = input('What is your name?: ')  # User input: '' (nothing)
>>>
>>> if name:
...     print(f'My name is... {name}')
... else:
...     print('Did you forget to type your name?')
Did you forget to type your name?

6.2.9. Use Case - 0x01

  • Cosmonaut

>>> country = 'Russia'
>>>
>>> if country == 'USA':
...     job = 'astronaut'
... else:
...     job = 'cosmonaut'
>>>
>>> print(job)
cosmonaut

6.2.10. Use Case - 0x02

  • Any

>>> data = [True, False, True]
>>>
>>> if any(data):
...     print('Yes')
... else:
...     print('No')
Yes

6.2.11. Use Case - 0x03

  • All

>>> data = [True, False, True]
>>>
>>> if all(data):
...     print('Yes')
... else:
...     print('No')
No

6.2.12. Assignments

Code 6.1. Solution
"""
* Assignment: Conditional If Underage/Adult
* Required: yes
* Complexity: easy
* Lines of code: 4 lines
* Time: 3 min

English:
    1. Ask user to input age
    2. Check whether user is adult (age equals or above 18)
    3. Run doctests - all must succeed

Polish:
    1. Poproś użytkownika o wprowadzenie wieku
    2. Sprawdź czy użytkownik jest pełnoletni (wiek równy lub powyżej 18)
    3. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `int()`
    * `>=`

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

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'

    >>> assert type(result) is str, \
    'Variable `result` has invalid type, should be str'

    >>> result in ('Adult', 'Young')
    True
"""

# Simulate user input (for test automation)
from unittest.mock import MagicMock
input = MagicMock(side_effect=['21'])


age = input('What is your age?: ')
ADULT = 18

# str: Whether 'Adult' or 'Young'
result = ...

Code 6.2. Solution
"""
* Assignment: Conditional If IPv4/IPv6
* Required: yes
* Complexity: easy
* Lines of code: 4 lines
* Time: 3 min

English:
    1. To `result: bool` assign whether `IP_ADDRESS` is IPv4 or IPv6 protocol:
       a. `IPv4` if dot `.` is in the IP address
       b. `IPv6` if dot `.` is not in the IP address
    2. Run doctests - all must succeed

Polish:
    1. Do `result: bool` przypisz czy `IP_ADDRESS` jest protokołu IPv4 czy IPv6:
       a. `IPv4` jeżeli jest kropka `.` w adresie IP
       b. `IPv6` jeżeli kropki nie ma
    2. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `in`

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

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'

    >>> assert type(result) is str, \
    'Variable `result` has invalid type, should be str'

    >>> assert result in ('IPv4', 'IPv6'), \
    'Variable `result` must be either `IPv4` or `IPv6`'
"""

IP_ADDRESS = '127.0.0.1'

# str: Whether 'IPv4' or 'IPv6'
result = ...