Space Python Loading Python…

Search the log

Write the codeMedium 2 stages

The mission log records thousands of lines a day, and the flight director only wants to see the errors. Before the log viewer can highlight them, it has to know whether a line contains ERROR, and where.

Search a log line, then pull out the error message that follows.

Print whether the line contains ERROR, and the index where it starts:

Contains ERROR: True
Starts at: 13

"ERROR" in line gives True or False. line.find("ERROR") gives the index of the first character of the match, or -1 if there's no match at all.

Search the line
Works for a line without an error

Finish stage 1 to unlock this stage.

The flight director wants just the message that comes after ERROR and the space after it. Add a line:

Contains ERROR: True
Starts at: 13
Message: Fuel valve 2 not responding

Start the slice from where ERROR starts, plus its length, plus one for the space, and leave the end out. The time in brackets can grow longer, [T+100:02:00], so work from position, not from a fixed number.

Print the message
Works when the time is longer
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

Methods find, count, replace and split

methods-find-replace-split.py
# Searching, counting, and splitting strings

# A snippet of mission log text
report = "Mars rover Curiosity landed. Curiosity is still active."

# .find() returns the index of the first match, or -1 if not found
position = report.find("Curiosity")
print("Curiosity first appears at index:", position)

missing = report.find("Voyager")
print("Voyager index:", missing)  # -1 means not found

# .count() returns how many times a substring appears
mentions = report.count("Curiosity")
print("Curiosity mentioned:", mentions, "times")

# .replace() can clean up or swap text - here we redact a name
public_report = report.replace("Curiosity", "[REDACTED]")
print(public_report)

# .split() breaks a string into a list, using a separator
crew_data = "Mark Watney,Botanist,Ares III,Sol 18"
fields = crew_data.split(",")
print("Fields:", fields)

# We can grab individual pieces by index
print("Name:", fields[0])
print("Role:", fields[1])

# Splitting on whitespace - no argument needed
sentence = "Houston we have a problem"
words = sentence.split()
print("Words:", words)
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:].