Space Python Loading Python…

Creating Dictionaries

 
0:00 / 0:00

Challenge

Practise what this example shows with these challenges.

Line to record

Write the codeEasy 2 stages

Extend the feed parser so it stores every reading as a dict with time, sensor, value and unit in a list of records, then let other code pick out one sensor's readings by key.

Start the challenge

API response

Write the codeEasy 2 stages

Print the name, provider and status of a launch from a dict shaped like a launch-API response, then list every field the response has with items(), the way you'd inspect an API you haven't used before.

Start the challenge

Missing field

Fix the codeEasy 3 stages

Reading launch["pad"] stops with a KeyError when the API hasn't assigned a pad yet. Predict it, check the keys with in, fix it with get() and a default, then print a list of launches with defaults for any missing field.

Start the challenge

Lookup table

Fix the codeMedium 2 stages

Replace a nine-branch elif that maps planets and the Moon to surface gravity with a dict and one get() lookup, keeping the output exactly the same, then loop over the dict for a weight table.

Start the challenge

Index by ID

Write the codeMedium 2 stages

Build a dict that maps every cargo ID to its record, so any item is found with one lookup instead of a search through the list, then answer a list of requests, including an ID that isn't on the manifest.

Start the challenge

Unit converter

Write the codeMedium 2 stages

Write convert(), which looks a unit pair up in a table of factors, works the other way round by dividing, and refuses unknown units or missing pairs with a clear ValueError, then extend the table with offsets for temperatures.

Start the challenge

Discussion

Stuck on this example or want to go further? Ask here, or see what others asked.

Log in to ask and reply.

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:].