Space Python Loading Python…

Shared default list

Fix the codeMedium 3 stages

Each sampling run should start a clean log. The morning run did. The evening run started with the morning's sample already in it, and by the end of the week every new log held every sample ever taken.

Predict what the program prints, find out where the old samples come from, fix the default, then write a function that gets it right from the start.

Don't run the code yet. Read it and decide what it will print on its first line. Write your guess in prediction, at the top, between the quotes. Then run it and compare.

Any guess passes this stage: the point is to think before you run. After you check, you'll see your prediction next to what Python printed.

The default list: is it made once, or on every call?

Predict, then run
Leave the program as it is

Finish stage 1 to unlock this stage.

Investigate. Check with morning is evening whether the two results are the same list, and store True or False in same_list. Work out how many samples a third call without a log would return, and store it in third_count. Remove any lines you added before you check.

Modify. Give each call without a log its own new list. It must print:

Morning: ['ENG-2 412.5']
Evening: ['CABIN 101.2']
Find the shared list
A new list per call

Finish stage 2 to unlock this stage.

Write new_record(sensor, value, flags=None), which returns a dict with a fresh flags list whenever none is given. The program flags the first record, and the second must stay clean:

{'sensor': 'ENG-2', 'value': 455.2, 'flags': ['check']}
{'sensor': 'CABIN', 'value': 101.2, 'flags': []}

Use the same None pattern; don't write flags=[].

Two independent records
No shared default
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:].