case2. Checking Specific Conditions
After loading the code file written by the student, scoring can be conducted based on whether a specific condition is met.
The following example is a scoring code for an exercise where students are required to write an acrostic poem using the print()
function three times.
The student's written file (main.py) will be checked to see if the print()
function is used a total of three times.
import os
import subprocess
import sys
sys.path.append(os.getcwd())
from grader_elice_utils import EliceUtils # isort:skip
elice_utils = EliceUtils()
elice_utils.secure_init()
try:
total_score = 0
# Code to check if the print() function is used 3 or more times
count = 0
with open("main.py") as ans:
datafile = ans.readlines()
for line in datafile:
if 'print' in line:
count += 1
if count >= 3:
total_score += 100
elice_utils.secure_send_grader('✅ Correct! You wrote a wonderful acrostic poem using print()! \n')
else:
elice_utils.secure_send_grader('❌ Incorrect! Please create an acrostic poem using print() three times.\n')
# Sending the final score for the exercise
total_score = int(total_score)
elice_utils.secure_send_score(total_score)
except Exception as err:
elice_utils.secure_send_grader('An error occurred during grading. Please check if your code runs correctly.\n')
elice_utils.secure_send_score(0)
sys.exit(1)