Space Python Loading Python…

Lookup table

Fix the codeMedium 2 stages

The mission planning tool tells astronauts how much they will weigh at their destination. Its gravity values live in a chain of nine elif branches, one per body, and every new destination means another branch.

Turn the chain into a table without changing a single character of the output, then use the table for something the chain could never do.

Replace the nine-branch elif chain with a dict called GRAVITY_M_S2 that maps each body to its gravity, and one lookup. The output must stay exactly the same:

Mars: 3.7 m/s², a 92.5 kg astronaut weighs 342 N

A body that isn't in the table still prints the no-data line: GRAVITY_M_S2.get(body) gives None for a missing key, just like the old else.

Keep the output
A table instead of a chain

Finish stage 1 to unlock this stage.

The table can do what the chain couldn't. After the line for the chosen body, print the astronaut's weight on every body in the table, rounded to whole newtons:

Mars: 3.7 m/s², a 92.5 kg astronaut weighs 342 N
All bodies:
  Mercury: 342 N
  Venus: 823 N
  Earth: 907 N
  Moon: 148 N
  Mars: 342 N
  Jupiter: 2137 N
  Saturn: 832 N
  Uranus: 805 N
  Neptune: 1018 N

Loop over GRAVITY_M_S2.items(); the weight is the mass times the gravity.

Print the table
Works for any astronaut
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:].