Space Python Loading Python…

Sensible defaults

Write the codeEasy 2 stages

Most commands to the spacecraft are sent the same way: three retries, five seconds to answer. Only a few need something else, like more retries for deploying the solar array, or a short timeout for an abort, where waiting is worse than failing.

Give the uplink function sensible defaults, then add an option without touching any of its callers.

Give retries a default of 3 and timeout a default of 5.0, so the three calls below the function print:

PING (retries: 3, timeout: 5.0 s)
DEPLOY_ARRAY (retries: 5, timeout: 5.0 s)
ABORT (retries: 3, timeout: 1.0 s)

Write the defaults in the def line: def send_command(cmd, retries=3, timeout=5.0):. The calls stay as they are.

Three calls
Defaults for any call

Finish stage 1 to unlock this stage.

Abort commands need to jump the queue. Add a priority parameter with a default of "normal", show it in the line, and add a fourth call:

PING (retries: 3, timeout: 5.0 s, priority: normal)
DEPLOY_ARRAY (retries: 5, timeout: 5.0 s, priority: normal)
ABORT (retries: 3, timeout: 1.0 s, priority: normal)
ABORT (retries: 3, timeout: 1.0 s, priority: high)

Put the new parameter last, so every call written before still means the same, and add print(send_command("ABORT", priority="high", timeout=1.0)) at the bottom.

Four calls
Old calls still work
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

Default Parameter Values

default-parameter-values.py
# A parameter can carry a default, used whenever you leave it out
def orbit_summary(satellite, altitude_km=550):
    return f"{satellite} at {altitude_km} km"

print(orbit_summary("Starlink-1130"))
print(orbit_summary("Sentinel-2", 786))

# Defaults let one function cover the common case and the unusual one
def link_budget(distance_au, watts=20, dish_m=70):
    return watts * dish_m / distance_au

print("Standard:", round(link_budget(1.5), 1))
print("Bigger transmitter:", round(link_budget(1.5, 100), 1))
print("Smaller dish too:", round(link_budget(1.5, 100, 34), 1))

# Defaults must come last. This would be a syntax error:
# def broken(altitude_km=550, satellite):
#     return satellite

# One strange rule: the default is built once, when def runs
def add_target(name, log=[]):
    log.append(name)
    return log

print(add_target("Vega"))
print(add_target("Altair"))


# The fix: default to None and build a fresh list inside
def add_target_safe(name, log=None):
    if log is None:
        log = []
    log.append(name)
    return log

print(add_target_safe("Vega"))
print(add_target_safe("Altair"))
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:].