I'm trying to create a CodeRunner quiz to assess a write-a-python-program assignment in which the students are asked to pass a number to the program through the command line, compute the reciprocal of that number, and handle any possible exceptions (such
as ZeroDivisionError
). A simplified solution would be as follows:
import sys try: number = float(sys.argv[1]) inv = 1/number print(f'The reciprocal of {number} is {inv}') except Exception as e: print(e)
And these are some of the tests I intend to perform:
$ python reciprocal.py list index out of range $ python reciprocal.py 0 division by zero $ python reciprocal.py 5 The reciprocal of 5 is 0.2
I'm struggling to come up with a template that suits my needs. This is what I tried so far (inspired on this post):
#!/usr/bin/env python import subprocess, sys student_answer = """{{ STUDENT_ANSWER | e('py') }}""" with open("reciprocal.py", "w") as src: print(student_answer, file=src) SEPARATOR = "#<ab@17943918#@>#" {% for TEST in TESTCASES %} bash_command = """{{ TEST.testcode | e('py') }}""" output = subprocess.check_output(bash_command.split()) print(output) {% if not loop.last %} print(SEPARATOR) {% endif %} {% endfor %}@17943918#@>
But I'm getting this error:
***Error*** Traceback (most recent call last): File "__tester__.python3", line 15, in output = subprocess.check_output(bash_command.split()) File "/usr/lib/python3.6/subprocess.py", line 336, in check_output **kwargs).stdout File "/usr/lib/python3.6/subprocess.py", line 403, in run with Popen(*popenargs, **kwargs) as process: File "/usr/lib/python3.6/subprocess.py", line 709, in __init__ restore_signals, start_new_session) File "/usr/lib/python3.6/subprocess.py", line 1344, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'python': 'python'
Any help would be greatly appreciated.