Space Python Loading Python…

Filter the log

Write the codeMedium 2 stages

The full mission log has every heater adjustment and wheel-speed check in it. The flight director's screen shows only the entries that need attention: warnings and errors.

Build that filtered list from the log, then make the filter adjustable, as real logging tools do.

Build a new list called alerts with only the WARN and ERROR entries, in log order. Then print how many there are, and each one:

Alerts: 4
[T+00:12:05] WARN  Battery 3 temperature high
[T+00:14:47] ERROR Fuel valve 2 not responding
[T+00:15:42] WARN  Star tracker lost lock
[T+00:16:03] ERROR Fuel valve 2 still closed

Start with alerts = [], loop over the log, and append() each line that contains "] WARN" or "] ERROR". The log itself must stay complete.

Filter the log
A new list, the log untouched

Finish stage 1 to unlock this stage.

Make the filter adjustable. Add the two lines below under the log, then keep every entry whose level is min_level or more serious, and use min_level in the first line of the output:

LEVELS = ["INFO", "WARN", "ERROR"]    # from least to most serious
min_level = "WARN"                    # show this level and above
WARN and above: 4
[T+00:12:05] WARN  Battery 3 temperature high
[T+00:14:47] ERROR Fuel valve 2 not responding
[T+00:15:42] WARN  Star tracker lost lock
[T+00:16:03] ERROR Fuel valve 2 still closed

The level is the second word of each entry: line.split()[1]. LEVELS.index(level) gives its rank, 0 for INFO up to 2 for ERROR, so compare the ranks.

Filter by level
Works for any level
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:].