Space Python Loading Python…

Flatten nested ifs

Fix the codeMedium 2 stages

The docking check for the station works, but it has grown into three if statements inside each other, with HOLD written out three times. Every new rule would push the code further to the right.

Flatten it into one condition without changing what it prints, then rewrite it so a HOLD says which check failed.

Docking is GO only when all three checks pass: the closing speed is under 0.1 m/s, the alignment is under 2 degrees, and the latches are ready. Replace the three nested if statements with one if that joins the checks with and, and one else. The output must not change:

Docking: GO

Whatever the readings are, your version has to print exactly what the nested one would.

Keep the output
One flat condition

Finish stage 1 to unlock this stage.

The crew wants to know why docking is on hold. Rewrite the check as one flat chain that tests for each problem in turn and reports the first one it finds; GO goes in the else at the end:

Docking: HOLD, closing speed too high
Docking: HOLD, misaligned
Docking: HOLD, latches not ready
Docking: GO

Check the speed first, then the alignment, then the latches. No if inside another if. With today's readings, it still prints GO.

Report the first failed check
Flat, not nested
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

Logical Operators and, or, not

logical-operators-and-or-not.py
# Logical operators: and, or, not

fuel_percent = 85
crew_ready = True
weather = "clear"

# AND - both conditions must be true
if fuel_percent > 70 and crew_ready:
    print("Fuel and crew: GO")

# OR - at least one must be true
if weather == "clear" or weather == "overcast":
    print("Weather: GO")

# NOT - flips True to False and vice versa
abort_signal = False

if not abort_signal:
    print("No abort signal received")

# Combining all three in a launch decision
if fuel_percent > 70 and crew_ready and not abort_signal:
    print("ALL SYSTEMS GO - clear for launch!")
else:
    print("HOLD - check failed conditions")

# Try changing values above and run again
# What happens with fuel_percent = 50?
# What happens with abort_signal = True?
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:].