Space Python Loading Python…

Count warnings

Write the codeMedium 3 stages

At the end of each orbit, the flight director reads a summary of the mission log: how many entries, and how many of them are warnings. A sudden jump in warnings is often the first sign that something is going wrong.

Count them, make sure the count can't be fooled, then summarise the log by level.

Count the entries in the log, one per line, and the WARN entries, then print:

Entries: 6
Warnings: 3

log.count("WARN") counts how many times the text appears. A multi-line string has a new line character, "\n", between its lines, so there is one line more than there are new lines.

Count entries and warnings
Count from the log

Finish stage 1 to unlock this stage.

A new entry has come in: INFO WARN light test complete. It's an INFO entry about the warning light, but the starter code now counts 4 warnings. Make the count match only the level, so it prints:

Entries: 7
Warnings: 3

Every level comes straight after the ] of the time and a space. Count that pattern instead of the bare word.

Count only the WARN level
Still counts from the log

Finish stage 2 to unlock this stage.

The orbit summary lists every level. Replace the warnings line with one line per level:

Entries: 7
INFO: 4
WARN: 3
ERROR: 0

Use the same pattern for each: "] INFO", "] WARN" and "] ERROR". A level that never appears counts as 0.

Count every level
Counts from the log
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:].