Space Python Loading Python…

Copy-pasted blocks

Fix the codeMedium 2 stages

The tank status display was written in a hurry: one block of code for tank A, then the same block copied for B and C. It works, but every change has to be made three times, and the next tank means a fourth copy.

Turn the copies into one loop without changing a single character of the output, then see how cheap the next tank becomes.

Replace the three copied blocks with a list of (name, level) pairs and one loop. Keep the three level variables, which come from telemetry. The output must stay exactly the same:

Tank A: 78.3%
Tank B: 12.5% LOW
Tank C: 46.0%

Build tanks = [("A", tank_a), ("B", tank_b), ("C", tank_c)], then loop with for name, level in tanks: and keep one copy of the if/else, with name in place of the letter.

Keep the output
One copy of the check

Finish stage 1 to unlock this stage.

A fourth tank has been fitted. Add tank_d = 9.75 under tank_c and one more pair to the list. Then, after the loop, list the low tanks, or none when there aren't any:

Tank A: 78.3%
Tank B: 12.5% LOW
Tank C: 46.0%
Tank D: 9.8% LOW
Low tanks: B, D

Collect the names of low tanks in a list inside the loop, and join them after it.

Add the tank
Still one copy of the check
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

For Loop Basics

for-loop-basics.py
# A for loop visits every item in a collection, one at a time
cargo = ["water", "food", "spare parts", "experiments"]

for item in cargo:
    print("Loading:", item)

print("Hold sealed")

# The loop body can do real work, not just print
masses_kg = [820, 640, 310, 175]
total = 0
for mass in masses_kg:
    total = total + mass
print("Total cargo:", total, "kg")

# A tuple of pairs unpacks straight into two variables
manifest = (("water", 820), ("food", 640), ("experiments", 175))
for name, mass in manifest:
    print(f"{name}: {mass} kg")

# A string is a collection too, one character at a time
for letter in "CYGNUS":
    print(letter, end=" ")
print()

# A set works as well, but the order is never promised
for module in {"Unity", "Harmony", "Destiny"}:
    print("Docking check:", module)
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:].