Space Python Loading Python…

Paged results

Write the codeMedium 2 stages

The launch archive doesn't send all its results at once. Like most web APIs, it answers in pages: a few results per request, and you ask again for the next page. The mission planners want one list of every launch.

Merge the pages into one list, then fetch them the way a real client does, without knowing in advance how many there are.

Merge the pages into one list called launches, then print how many launches there are, and each one:

Launches: 7
Lumen-1: success
Kestrel-4: success
Aurora Relay: failure
Kestrel-5: success
Tern-2: success
Lumen-2: success
Polaris Cargo 3: success

Start with launches = [] and extend() it with each page. Then loop over launches, unpacking each pair: for mission, outcome in launches:.

Merge the pages
One flat list, any pages

Finish stage 1 to unlock this stage.

A real client doesn't know how many pages there are. It asks for one page at a time and stops after the first page with fewer than PAGE_SIZE results. Add PAGE_SIZE = 3 under the pages, then replace the for loop with a while loop that takes pages[0], pages[1] and so on, prints each page's size as it arrives, and stops after the short one:

Page 1: size 3
Page 2: size 3
Page 3: size 1
Launches: 7
Lumen-1: success
Kestrel-4: success
Aurora Relay: failure
Kestrel-5: success
Tern-2: success
Lumen-2: success
Polaris Cargo 3: success

while True: with a break after the size check fits here: the loop only knows it is done once it has seen the page.

Fetch page by page
Stops at the right page
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

While Loop Basics

while-loop-basics.py
# A while loop repeats for as long as its condition stays true
passes = 3
while passes > 0:
    print("Aerobraking pass,", passes, "to go")
    passes = passes - 1
print("Sequence finished")

# The real power: you do not need to know how many turns it will take
altitude_km = 45000
target_km = 400
orbits = 0
while altitude_km > target_km:
    altitude_km = altitude_km * 0.7
    orbits = orbits + 1
print("Took", orbits, "orbits to reach", round(altitude_km), "km")

# A while loop can also watch a true or false flag
approaching = True
distance_m = 20
while approaching:
    print("Range:", distance_m, "m")
    distance_m = distance_m - 8
    if distance_m <= 0:
        approaching = False
print("Soft capture confirmed")

# Forget to change the condition and the loop never ends
# passes = 3
# while passes > 0:
#     print("This one never stops")
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:].