Space Python Loading Python…

JSON out

Write the codeEasy 2 stages

At the end of each day, the mission dashboard sends a summary to the archive, and the archive only takes JSON. The summary already exists as a Python dict. Printing it with print(summary) looks close, but single quotes, True and None aren't JSON, and the archive rejects it.

Produce real JSON text, laid out so a person can read it too.

Turn summary into JSON text indented by 2 spaces, store it in text, and print it:

{
  "mission": "Lumen-3",
  "agency": "JAXA",
  "ground_station": "Toru\u0144",
  "orbits": 12,
  "max_temp_c": 455.2,
  "alerts": [
    "Battery 3 temperature high",
    "Star tracker lost lock"
  ],
  "crewed": false,
  "next_pass": null
}

Use json.dumps(summary, indent=2). Look at what False, None and Toruń turned into.

Indented JSON
Real JSON in text

Finish stage 1 to unlock this stage.

Keep Toruń readable with ensure_ascii=False, and sort the keys with sort_keys=True, so that two days' files line up when compared:

{
  "agency": "JAXA",
  "alerts": [
    "Battery 3 temperature high",
    "Star tracker lost lock"
  ],
  "crewed": false,
  "ground_station": "Toruń",
  "max_temp_c": 455.2,
  "mission": "Lumen-3",
  "next_pass": null,
  "orbits": 12
}
Readable and sorted
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:].