Space Python Loading Python…

Line to record

Write the codeEasy 2 stages

The parser from "Skip blanks and comments" prints each reading and forgets it. The rest of the ground software needs the readings themselves: the alert check, the charts, the daily report. Each one should be able to ask a reading for its sensor or its value by name.

Store every reading as a record, then use the records to answer an engineer's question.

Instead of printing each reading, store it as a dict in a list called records, then print how many there are and each record:

Records: 4
{'time': '09:30:05', 'sensor': 'ENG-2', 'value': 412.5, 'unit': 'C'}
{'time': '09:30:05', 'sensor': 'TANK-1', 'value': 78.3, 'unit': '%'}
{'time': '09:30:10', 'sensor': 'CABIN', 'value': 101.2, 'unit': 'kPa'}
{'time': '09:30:10', 'sensor': 'ENG-2', 'value': 415.0, 'unit': 'C'}

Start with records = []. In the loop, build {"time": timestamp[11:19], "sensor": sensor, "value": float(value), "unit": unit} and append it. The value is a number in the record, so it prints without quotes.

Store the records
Records the rest of the program can use

Finish stage 1 to unlock this stage.

An engineer wants the ENG-2 readings. Add wanted = "ENG-2" under the feed, keep the count line, and instead of printing every record, print only the records of the wanted sensor. If there are none, say so:

Records: 4
ENG-2 at 09:30:05: 412.5 C
ENG-2 at 09:30:10: 415.0 C

With wanted = "GYRO-X", the last line is No readings from GYRO-X.

Read each record by key: record["sensor"], record["time"] and so on. Inside an f-string, use the other kind of quotes: {record['time']}.

Pick out one sensor
Works for any sensor
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

Creating Dictionaries

creating-dictionaries-python.py
# A dictionary stores pairs: a key, then its value
webb = {
    "name": "James Webb Space Telescope",
    "mirror_metres": 6.5,
    "launched": 2021,
    "instruments": 4,
    "infrared": True,
}
print(webb)

# Square brackets look up a value by its key
print("Mirror width:", webb["mirror_metres"], "metres")

# get() returns a fallback instead of crashing on a missing key
print("Cost:", webb.get("cost", "not recorded"))

# The dict() function builds the same thing from keyword arguments
hubble = dict(name="Hubble", mirror_metres=2.4, launched=1990)
print("Hubble mirror:", hubble["mirror_metres"], "metres")

# Keys do not have to be text. Anything unchangeable works
launch_log = {
    1990: "Hubble",
    2021: "Webb",
    (2027, "planned"): "Roman",
}
print(launch_log[1990])
print("Missions logged:", len(launch_log))
print("Anything launched in 2021?", 2021 in launch_log)
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:].