Space Python Loading Python…

Manifest line

Write the codeEasy 2 stages

Every item in the cargo manifest is stored as a tuple: its ID, what it is and its mass. The loading report shows one line per item, with the fields in a fixed layout the crew knows by heart.

Unpack an item and print its line, then cope with the manifest gaining a new field.

Unpack the tuple into three variables and print one manifest line, with the mass to one decimal place:

CG-0042 | Water filter cartridges | 18.5 kg

Three names on the left of =, one for each value, in order.

Print the manifest line
Works for any item

Finish stage 1 to unlock this stage.

The manifest now also records the module each item is stowed in. The starter code stops with ValueError: too many values to unpack, because the tuple has four values and only three names. Fix the unpacking and add the module to the line:

CG-0042 | Water filter cartridges | 18.5 kg | Node 2
Unpack all four fields
Works for any item
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

Tuple Unpacking

tuple-unpacking.py
# Tuple unpacking: one line, many variables

# New Horizons flyby of Pluto, 14 July 2015
flyby = ("New Horizons", "Pluto", 12472)
probe, target, closest_km = flyby
print(f"{probe} passed {target} at {closest_km} km")

# Asterisk grabs everything that is left over
ares3 = ("Lewis", "Johanssen", "Martinez", "Vogel", "Beck", "Watney")
commander, *crew, botanist = ares3
print("Commander:", commander)
print("Crew:", crew)
print("Left behind on Mars:", botanist)

# Swapping two values without a temporary variable
primary_dish, backup_dish = "Goldstone", "Canberra"
primary_dish, backup_dish = backup_dish, primary_dish
print(f"Now transmitting from {primary_dish}, backup is {backup_dish}")

# Underscore marks a value you do not need
sample = ("Ryugu", 2020, 5.4)
asteroid, _, grams = sample
print(f"Hayabusa2 returned {grams} grams of {asteroid}")

# Unpacking inside a for loop
moons = [("Titan", 5150), ("Enceladus", 504), ("Iapetus", 1469)]
for name, diameter_km in moons:
    print(f"{name}: {diameter_km} km across")
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:].