Space Python Loading Python…

Print the manifest

Write the codeEasy 2 stages

In "Manifest line" you printed one item from a tuple. The logistics system now exports the whole manifest as a list of dicts, one per item, with an ID, a name, a category and a mass. The loading crew wants it printed, one line per item, with the total at the bottom.

Print one manifest line for every item, with the mass to one decimal place:

CG-0042 | Water filter cartridges | 18.5 kg
CG-0043 | Spare cabin fan | 2.4 kg
CG-0044 | Freeze-dried meals | 42.0 kg
CG-0045 | CO2 scrubber cartridges | 31.3 kg
CG-0046 | Tablet computers | 3.4 kg
CG-0047 | Rice and lentils | 25.0 kg

Loop over cargo; each record is a dict, so record["id"], record["item"] and record["mass_kg"] give its fields.

Print every item
Works for any manifest

Finish stage 1 to unlock this stage.

Add up the masses in the same loop and print a total line at the bottom:

CG-0042 | Water filter cartridges | 18.5 kg
CG-0043 | Spare cabin fan | 2.4 kg
CG-0044 | Freeze-dried meals | 42.0 kg
CG-0045 | CO2 scrubber cartridges | 31.3 kg
CG-0046 | Tablet computers | 3.4 kg
CG-0047 | Rice and lentils | 25.0 kg
Total: 122.6 kg in 6 items

Start total_kg = 0 before the loop and add record["mass_kg"] to it inside.

Add a total
Works for any manifest
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

Iterating Over Dictionary

iterating-over-dictionary.py
# How long a single day lasts on each planet, measured in Earth hours
day_hours = {
    "Mercury": 1408,
    "Venus": 5832,
    "Earth": 24,
    "Mars": 24.6,
    "Jupiter": 9.9,
}

# A plain for loop walks the keys
for planet in day_hours:
    print("Planet:", planet)

# .values() walks the values instead
total = 0
for hours in day_hours.values():
    total = total + hours
print("All five days added together:", total, "hours")

# .items() hands you the key and the value at the same time
for planet, hours in day_hours.items():
    print(f"{planet}: one day is {round(hours / 24, 2)} Earth days")

# sorted() walks the same keys in a different order
for planet in sorted(day_hours):
    print(planet, "->", day_hours[planet], "hours")
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:].