Hello All,
I would like to get the programs with validation working in C# with Coderunner:
Enter shape to draw: Sqare
Invalid input. Try again. Enter shape to draw: Square Filled or unfilled? Filed Invalid input. Try again. Filled or unfilled? Filled Size: 200 Invalid input. Try again. Size: -1 Invalid input. Try again. Size: 1
I know that in the examples provided, for Python the template additions below work.
__saved_input__ = input def input(prompt=""): result = __saved_input__(prompt) print(result) return result {{STUDENT_ANSWER}} {{TEST.testcode}}
I am not entirely sure how the above works for Python. I would appreciate help with how Console.ReadLine(); in C# could be re-written.
The template I am using for C# is listed below.
Thank you
""" The template for a question type that compiles and runs a student-submitted
mono C# program.
"""
import subprocess, sys
# Write the student code to a file prog.cs
student_answer = """{{ STUDENT_ANSWER | e('py') }}"""
with open("prog.cs", "w") as src:
print(student_answer, file=src)
# Compile
return_code = subprocess.call(['mcs', 'prog.cs'])
if return_code != 0:
print("** Compilation failed. Testing aborted **", file=sys.stderr)
# If compile succeeded, run the code. Since this is a per-test template,
# stdin is already set up for the stdin text specified in the test case,
# so we can run the compiled program directly.
if return_code == 0:
try:
output = subprocess.check_output(["mono", "./prog.exe"], universal_newlines=True)
print(output)
except subprocess.CalledProcessError as e:
if e.returncode > 0:
# Ignore non-zero positive return codes
if e.output:
print(e.output)
else:
# But negative return codes are signals - abort
if e.output:
print(e.output, file=sys.stderr)
if e.returncode < 0:
print("Task failed with signal", -e.returncode, file=sys.stderr)
print("** Further testing aborted **", file=sys.stderr)