Space Python Loading Python…

Real JSON

Write the codeEasy 2 stages

In "API response", the launch arrived as a ready-made Python dict, with a comment saying the JSON had already been turned into one. That was a shortcut. A real API sends text, and the first thing any program does with it is turn that text into data.

Do that step yourself with the json module, then deal with a response that didn't arrive in one piece.

The prints from "API response" expect a dict, but response_text is text, and the program stops with TypeError: string indices must be integers. Turn the text into a dict with json.loads(), then add a line for crewed:

Launch: Lumen-3
Provider: JAXA
Status: Go
Crewed: False

import json is already at the top; replace launch = response_text with launch = json.loads(response_text).

Read the JSON
With the json module

Finish stage 1 to unlock this stage.

Three responses came in one batch, and the second was cut off in transfer. Loop over responses, print one line per launch, and print a message instead of crashing for the broken one:

Lumen-3 (JAXA): Go
Bad response: Unterminated string starting at
Kestrel-7 (ISRO): Hold

Catch json.JSONDecodeError; its msg attribute is the short reason, without the position.

Two launches and one message
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

Import System Modules

import-system-modules.py
# A module is a toolbox someone else already built and tested for you

# Plain import brings in the whole module
import sys
import platform
import math

# from ... import brings in just one tool
from math import sqrt

# as gives a module a shorter nickname
import random as rng

# With a plain import: module name, a dot, then the tool
print("Python version:", platform.python_version())
print("Running on:", platform.system())
print("Python lives at:", sys.executable)

# math carries constants you would not want to type from memory
radius_km = 6371
print("Earth circumference:", round(2 * math.pi * radius_km), "km")

# sqrt arrived on its own, so it needs no dot in front
print("Square root of 9:", sqrt(9))

# rng is simply a nickname for random
print("Random sol of the Martian year:", rng.randint(1, 687))
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:].