Skip to content
Foxipa
Code Completion 4 Challenges

Complete the environment configuration code

Practise optional lookups, required settings, numeric validation, and explicit Boolean parsing.

Intermediate ~10 min read

Challenge 1

Read an optional theme setting and use "light" only when APP_THEME is absent.

python Your answer
import os

# The launcher can supply APP_THEME.
theme = 
print(theme)

Press Ctrl⌘Enter to check your answer

Challenge 2

Retrieve a required value from an injected environment mapping and reject missing or whitespace-only values.

python Your answer
def required_setting(environ, name):
    value = 
    if value is None or not value.strip():
        raise ValueError(f"{name} must be set")
    return value.strip()

print(required_setting({"APP_REGION": " eu-west "}, "APP_REGION"))

Press Ctrl⌘Enter to check your answer

Challenge 3

Convert a supplied text setting to an integer before the provided range check.

python Your answer
environ = {"APP_PORT": "8080"}
raw = environ.get("APP_PORT", "8000")
port = 
if not 1 <= port <= 65535:
    raise ValueError("APP_PORT is outside the allowed range")
print(port)

Press Ctrl⌘Enter to check your answer

Challenge 4

Complete a Boolean parser that recognizes explicit true/false spellings and rejects anything else.

python Your answer
def read_flag(environ, name, default=False):
    raw = environ.get(name)
    if raw is None:
        return default
    normalized = raw.strip().casefold()
    

print(read_flag({"APP_DEBUG": "yes"}, "APP_DEBUG"))
print(read_flag({"APP_DEBUG": "off"}, "APP_DEBUG"))

Press Ctrl⌘Enter to check your answer