9.2. Block Else

  • Unconditional Alternative

  • Optional

  • Executed when condition is not met

9.2.1. Syntax

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

9.2.2. SetUp

Simulate user input (for test automation)

>>> from unittest.mock import MagicMock
>>> input = MagicMock(side_effect=[''])

9.2.3. Oneline Block

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

9.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

9.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

9.2.6. Value Check

>>> age = 42
>>>
>>> if age >= 18:
...     print('adult')
... else:
...     print('minor')
...
adult

Note, that in Poland you are an adult when above 18 years old.

9.2.7. Conditional Assignment

>>> age = 42
>>>
>>> if age >= 18:
...     adult = True
... else:
...     adult = False
>>>
>>> print(adult)
True

Note, that in Poland you are an adult when above 18 years old.

9.2.8. Checking If Empty

>>> age = input('What is your age?: ')  #input: '' (nothing)
>>>
>>> if not age:
...     print('Did you enter your age correctly?')
...
Did you enter your age correctly?

9.2.9. Membership

>>> admins = ['mwatney', 'mlewis', 'rmartinez']
>>> login = 'avogel'
>>> if login in admins:
...     is_admin = True
... else:
...     is_admin = False

9.2.10. Use Case - 0x01

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

9.2.11. Use Case - 0x01

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

9.2.12. Use Case - 0x02

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

9.2.13. Use Case - 0x03

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

9.2.14. Assignments

Code 9.1. Solution
"""
* Assignment: Conditional If Adult
* Type: class assignment
* Complexity: easy
* Lines of code: 4 lines
* Time: 3 min

English:
    1. The user entered his/her age: 30
    2. Define variable `result: str` with value:
        a. 'junior' - if age is below 18
        b. 'senior' - if age is 18 or above
    3. Use if-else block
    4. Run doctests - all must succeed

Polish:
    1. Użytkownik podał swój wiek: 30
    2. Zdefiniuj zmienną `result: str` z wartością:
       a. 'junior' - jeżeli wiek jest poniżej 18 lat
       b. 'senior' - jeżeli wiek jest 18 lat lub więcej
    3. Użyj bloku if-else
    4. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `<`

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

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

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

    >>> assert result in {'junior', 'senior'}, \
    'Variable `result` should be either junior or senior'

    >>> pprint(result)
    'senior'
"""

AGE = 30


# Define variable `result: str` with value:
# - 'junior' - if age is below 18
# - 'senior' - if age is 18 or above
# Use if-else block
# type: str
result = ...


Code 9.2. Solution
"""
* Assignment: Conditional If IPv4/IPv6
* Type: class assignment
* Complexity: easy
* Lines of code: 4 lines
* Time: 3 min

English:
    1. To `result: str` 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: str` 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`
    * In IPv6 address there is no dot `.`
    * IPv4 example: 127.0.0.1
    * IPv6 example: 2001:0db8:85a3:0000:0000:8a2e:0370:7334

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

    >>> assert result is not Ellipsis, \
    'Assign your 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'

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

Code 9.3. Solution
"""
* Assignment: Conditional If IPv4/IPv6
* Type: class assignment
* Complexity: easy
* Lines of code: 4 lines
* Time: 3 min

English:
    1. To `result: str` 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: str` 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`
    * In IPv6 address there is no dot `.`
    * IPv4 example: 127.0.0.1
    * IPv6 example: 2001:0db8:85a3:0000:0000:8a2e:0370:7334

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

    >>> assert result is not Ellipsis, \
    'Assign your 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'

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