Space Python Loading Python…

Confirm by name

Write the codeMedium 2 stages

An abort can't be undone. The old abort(reason, confirm) could be called as abort("Test", True), where nothing on the line says what that True means, and a careless copy and paste could abort a real mission.

Make the confirmation impossible to give by accident.

Change the def line to abort(reason, *, confirm), so confirm can only be passed by name and can't be left out, and return the refusal when it isn't true. The calls print:

ABORT: Cabin pressure falling
Abort refused: pass confirm=True

With the * in place, abort("Test", True) stops with a TypeError, and so does abort("Test").

Confirm or refuse
Only by name, never left out

Finish stage 1 to unlock this stage.

Someone passed confirm="yes", and since a non-empty string is true, the abort ran. Accept only True itself, and add the call that tried:

ABORT: Cabin pressure falling
Abort refused: pass confirm=True
Abort refused: pass confirm=True

Compare with is not True instead of not confirm.

Refuse truthy values
Only True confirms
You're not logged in, so your progress is kept in this browser only. Log in to save it, and to use hints and solutions.

Hints

Combining Different Argument Types

combining-argument-types.py
# The order is fixed: normal, then *args, then defaults, then **kwargs
def mission_log(name, *events, priority="normal", **details):
    print(name, "| priority:", priority)
    for event in events:
        print("    event:", event)
    for key, value in details.items():
        print("   ", key, "=", value)
    print()

# Only the required one. Everything else falls back
mission_log("Rosalind Franklin")

# Two loose values, so they land in events
mission_log("Chandrayaan-3", "launch", "soft landing")

# priority sits after the star, so it can only be set by name
mission_log("Chandrayaan-3", "launch", "soft landing", priority="high")

# Careful: passing it positionally just makes it another event
mission_log("Chang'e 6", "launch", "high")

# Anything else you name drops into details
mission_log("Chang'e 6", "sample sealed", priority="high", site="far side", grams=1935)

# Both stars at the call site, working together
timeline = ["launch", "orbit insertion"]
extra = {"agency": "ISRO", "year": 2023}
mission_log("Chandrayaan-3", *timeline, priority="high", **extra)
Open the full example
script.py
PythonLn 1, Col 1Spaces: 4
Loading the Python environment…

Separate arguments with commas. They're passed on every Run as sys.argv[1:].