Space Python Loading Python…

Retry the uplink

Write the codeMedium 2 stages

Commands to the spacecraft don't always get through the first time: the radio can time out or lose the carrier. The uplink software tries again, but only a few times, so that a dead link doesn't block everything else.

Write the retry loop against a simulator that plays back the radio's replies, then space the attempts out.

Try up to MAX_ATTEMPTS times. Attempt 1 gets the first result from the list, attempt 2 the second, and so on. Print each attempt, stop at the first "ok", then say how it ended:

Attempt 1: timeout
Attempt 2: no carrier
Attempt 3: ok
Uplink sent on attempt 3

When all three fail, the last line is Uplink failed after 3 attempts. Loop over range(1, MAX_ATTEMPTS + 1), break on success, and use a flag such as sent to tell the two endings apart.

Retry until it works
Stops at the limit

Finish stage 1 to unlock this stage.

Retrying at once rarely helps a struggling link. Wait 2 seconds after the first failure, then twice as long after each next one, but don't wait after the last attempt, since nothing follows it:

Attempt 1: timeout
Waiting 2 s
Attempt 2: no carrier
Waiting 4 s
Attempt 3: ok
Uplink sent on attempt 3

Start delay_s = 2 before the loop. After a failure, if attempt < MAX_ATTEMPTS, print the wait and double delay_s. (The simulator only prints the wait; real code would call time.sleep(delay_s).)

Wait between attempts
No wait after the last try
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

Break and Continue in Loops

break-continue-loops.py
# break stops the loop the instant it runs
readings = [12, 45, 88, 210, 350, 190]
for value in readings:
    print("Checking flux:", value)
    if value > 200:
        print("Flare detected. Scan stopped.")
        break

# continue skips the rest of this pass and jumps to the next one
samples = [3.4, -1, 5.8, -1, 7.2]
total = 0
good = 0
for sample in samples:
    if sample == -1:
        print("Dropout, skipping this one")
        continue
    total = total + sample
    good = good + 1
print("Averaged", good, "good samples:", round(total / good, 2))

# while True runs forever on purpose, and break is the only way out
frequency = 1420
while True:
    print("Listening at", frequency, "MHz")
    frequency = frequency + 5
    if frequency > 1435:
        print("Band swept. Receiver off.")
        break
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:].