Prove the fix
The shared-list bug in add_sample() is fixed. How do you know it stays fixed? Six months from now someone will tidy the function, and log=[] looks so much simpler than the None check.
Write the test that proves the fix: it has to pass now and fail on the old code. The hidden tests put the old code back to check that it does.
Finish stage 1 to unlock this stage.
Write assert checks that pass on this function but would fail on the old one: call it twice without a log, and check each call got a list with only its own sample. Then print:
All checks passed
Leave the function as it is. The hidden tests swap it for the old version with log=[], and your checks must stop that one.
Finish stage 1 to unlock this stage.
A fix can go too far. Add checks that a log passed in still gets the sample: the result holds the old samples and the new one, and it is the very list that was passed in. Keep the passed line at the end.
The hidden tests now also try two "fixes" that break this: one that ignores the log it is given, and one that returns a copy instead of adding to it.
Hints
Default Parameter Values
# 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"))
Separate arguments with commas. They're passed on every Run as sys.argv[1:].