[PYTHON] Stage "Run a program #IP1"

I’m stuck on Stage #IP1. I can run the program but confused as to why its printing in a different format than what the test environment is asking for.

I’ve tried forcing a print command but doesn’t say what the test environment asks for.

Here are my logs:

[tester::#IP1] Running tests for Stage #IP1 (Run a program)
[tester::#IP1] [setup] export PATH=/tmp/quz:$PATH
[tester::#IP1] [setup] Available executables:
[tester::#IP1] [setup] - custom_exe_2866
[tester::#IP1] [setup] - custom_exe_3744
[tester::#IP1] Running ./your_program.sh
[your-program] $ custom_exe_2866 David David
[your-program] Program was passed 3 args (including program name).
[your-program] Arg #0 (program name): custom_exe_2866
[your-program] Arg #1: David
[your-program] Arg #2: David
[your-program] Program Signature: 3035542082
[tester::#IP1] ✓ Received expected response
[your-program] $ custom_exe_3744 Alice Maria Alice
[your-program] Program was passed 4 args (including program name).
[your-program] Arg #0 (program name): custom_exe_3744
[your-program] Arg #1: Alice
[your-program] Arg #2: Maria
[your-program] Arg #3: Alice
[your-program] Program Signature: 1111191913
[tester::#IP1] ✓ Received expected response
[your-program] $ 
[tester::#IP1] Test passed.

.....

[tester::#PN5] Running tests for Stage #PN5 (Implement exit)
[tester::#PN5] Running ./your_program.sh
[your-program] $ invalid_mango_command
[your-program] /bin/sh: invalid_mango_command: not found
[tester::#PN5] ^ Line does not match expected value.
[tester::#PN5] Expected: "invalid_mango_command: command not found"
[tester::#PN5] Received: "/bin/sh: invalid_mango_command: not found"
[your-program] $ 
[tester::#PN5] Assertion failed.
[tester::#PN5] Test failed

And here’s my code:

import sys
import os
import shutil
import subprocess

def exit_cmd(args):  # exit command
    sys.exit(0)

def echo_cmd(args):  # echo command
    echo = ' '.join(args[1:])  # Join arguments after 'echo'
    print(echo)

def type_cmd(args):  # type command
    command_name = args[1]  # Get the command name to check
    if command_name in builtin:  # check if command is builtin
        print(f"{command_name} is a shell builtin")
    else:
        path = shutil.which(command_name)
        if path:
            print(f"{command_name} is {path}")
        else:
            print(f"{command_name}: not found")

builtin = {"exit": exit_cmd, "echo": echo_cmd, "type": type_cmd}  # list of builtin commands

def main():
    while True:
        sys.stdout.write("$ ")
        command = input().strip()
        if not command:
            continue

        parts = command.split()
        cmd = parts[0]

        if cmd in builtin:
            builtin[cmd](parts)
        else:  # builtin commands not found
            try:  # run the command
                subprocess.run(command, shell=True)
            except FileNotFoundError:
                print(f"{cmd}: command not found")
            except subprocess.CalledProcessError as e:
                print(f"{cmd}: command not found")


if __name__ == "__main__":
    main()

Hey @zbm-2, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

What happens if you remove the shell=True part?

1 Like

when i removed shell=True the program fails to run so errors out during stage #IP1

@zbm-2 I’d suggest removing shell=True.

When shell=False (which is the default), subprocess.run expects the first argument to be a list of strings rather than a single string:

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.