Skip to content
Foxipa
Code Completion 4 Challenges

Complete the command-line stream and exit-status code

Practise accessing argument tokens, routing diagnostics, reading standard input, and propagating a command exit status.

Intermediate ~10 min read

Challenge 1

Use the slice that excludes the launch entry so the program can inspect its own command-line arguments.

python Your answer
import sys

arguments = 
print(arguments)

Press Ctrl⌘Enter to check your answer

Challenge 2

Write the warning to the diagnostic stream without mixing it into normal result output.

python Your answer
import sys

print("Report ready")
print("One record was skipped", )

Press Ctrl⌘Enter to check your answer

Challenge 3

Count the non-empty lines in a supplied text stream; ignore lines containing only whitespace.

python Your answer
from io import StringIO

source = StringIO("oak\n  \npine\n")
count = sum(1 for line in source if )
print(count)

Press Ctrl⌘Enter to check your answer

Challenge 4

Pass main()’s return value to the interpreter so the command exits with the selected status.

python Your answer
import sys

def main(arguments):
    if not arguments:
        print("A filename is required.", file=sys.stderr)
        return 2
    print(f"Queued: {arguments[0]}")
    return 0

if __name__ == "__main__":
    

Press Ctrl⌘Enter to check your answer