10.6. Conditional Recap

  • not ... - negation

  • ... and ... - conjunction

  • ... or ... - disjunction

  • Precedence: not, and, or

  • In boolean algebra use round brackets ( and ) to make code more readable

  • Block statements: if, elif, else

  • Python uses indentation instead of braces (convention: 4 spaces per indent level)

  • Code indented on the same level belongs to block

  • PEP 8 -- Style Guide for Python Code: 4 spaces indentation

  • Python throws IndentationError exception on problem

10.6.1. Assignments

# %% About
# - Name: Conditional Recap Auth
# - Difficulty: easy
# - Lines: 4
# - Minutes: 8

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Write authentication system
# 2. User will provide two strings: `USERNAME` and `PASSWORD`
# 3. Check if `USERNAME` is in the `DATABASE` and if the `PASSWORD` matches
# 4. If both matches, then define variable `result` with value `True`
# 5. Run doctests - all must succeed

# %% Polish
# 1. Napisz system uwierzytelniania
# 2. Użytkownik poda dwa ciągi znaków: `USERNAME` i `PASSWORD`
# 3. Sprawdź czy `USERNAME` jest w `DATABASE` i czy `PASSWORD` pasuje
# 4. Jeżeli oba pasują, to zdefiniuj zmienną `result` z wartością `True`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# TODO: Write expected result

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is bool, \
'Variable `result` has an invalid type; expected: `bool`.'

>>> from pprint import pprint
>>> pprint(result)
True
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports

# %% Types
result: bool

# %% Data
DATABASE = {
    'alice': 'secret',
    'bob': 'qwerty',
    'carol': '123456',
    'dave': 'abc123',
    'eve': 'password1',
    'mallory': 'NULL',
}

USERNAME = 'alice'
PASSWORD = 'secret'

# %% Result
result = ...