Space Python Loading Python…

Silent branch

Fix the codeEasy 3 stages

The public launch board colours each launch by its status: green for Go, amber for Hold, red for Scrub. Go and Hold look right. Scrubbed launches show no colour at all, and nobody noticed until a launch was scrubbed on live television.

Predict what the board prints, find out which statuses fall through, fix the function, then write one where no path is left without an answer.

Don't run the code yet. Read it and decide what it will print on its last 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.

Follow "Scrub" through the function to its end.

Predict, then run
Leave the program as it is

Finish stage 1 to unlock this stage.

Investigate. Store what status_light("Scrub") and status_light("TBD") give back, as text, in scrub_gives and unknown_gives.

Modify. Make every path through the function return a colour: red for Scrub, and grey for any other status. The board now lists TBD too, and must print:

Go: green
Hold: amber
Scrub: red
TBD: grey
Find the silent paths
Return on every path

Finish stage 2 to unlock this stage.

Write battery_state(percent): full from 80% up, ok from 20% up, and low below that. Make sure every path returns. Then print:

95%: full
80%: full
41%: ok
20%: ok
7%: low

Check from the top down and return as soon as you know the answer; the last line of the function covers everything left.

Print the battery states
Every path returns
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

Early Exit from Function

early-exit-from-function.py
# Check the deal breakers first and leave the moment one fails
def launch_check(wind_kph, fuel_pct, crew_ready):
    if wind_kph > 40:
        return "HOLD: wind too strong"
    if fuel_pct < 95:
        return "HOLD: fuel low"
    if not crew_ready:
        return "HOLD: crew not ready"
    return "GO for launch"

print(launch_check(18, 99, True))
print(launch_check(52, 99, True))
print(launch_check(18, 80, False))

# A bare return simply stops the function and hands back None
def log_anomaly(code):
    if code == 0:
        return
    print("Anomaly", code, "logged")

log_anomaly(0)
log_anomaly(7)

# Inside a loop, return leaves the loop and the function together
def first_clear_window(forecasts):
    for day, clouds in forecasts:
        print("checking", day)
        if clouds < 20:
            return day
    return "no window this week"

week = [("Mon", 80), ("Tue", 45), ("Wed", 10), ("Thu", 5)]
print("Launch on:", first_clear_window(week))
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:].