Space Python Loading Python…

First error

Write the codeEasy 2 stages

When something goes wrong on board, the engineers look for the first error in the log: later errors are often just consequences of the first one. On a long log, there's no point reading on once it's found.

Find the first error, stop the search there, and report what the search did.

Find the first ERROR entry, print its line number, counting from 1, with the entry itself, and stop searching there:

First error on line 4: [T+00:14:47] ERROR Fuel valve 2 not responding

Loop with enumerate(log, start=1). Look for "] ERROR", as in "Count warnings", so a message that only mentions the word doesn't count. break ends the loop.

Find the first error
Works for any log

Finish stage 1 to unlock this stage.

Show how much of the log the search needed: count the lines it checked and print the total after the loop. When there is no error at all, print No errors first:

First error on line 4: [T+00:14:47] ERROR Fuel valve 2 not responding
Checked 4 of 6 lines

With no errors in the log:

No errors
Checked 6 of 6 lines

Set checked = 0 and found = False before the loop. Inside it, add 1 to checked for every line, and set found = True just before the break.

Count the lines checked
Works with no errors
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

Break and Continue in Loops

break-continue-loops.py
# break stops the loop the instant it runs
readings = [12, 45, 88, 210, 350, 190]
for value in readings:
    print("Checking flux:", value)
    if value > 200:
        print("Flare detected. Scan stopped.")
        break

# continue skips the rest of this pass and jumps to the next one
samples = [3.4, -1, 5.8, -1, 7.2]
total = 0
good = 0
for sample in samples:
    if sample == -1:
        print("Dropout, skipping this one")
        continue
    total = total + sample
    good = good + 1
print("Averaged", good, "good samples:", round(total / good, 2))

# while True runs forever on purpose, and break is the only way out
frequency = 1420
while True:
    print("Listening at", frequency, "MHz")
    frequency = frequency + 5
    if frequency > 1435:
        print("Band swept. Receiver off.")
        break
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:].