case2. Check Specific Conditions

After importing the code file written by the student, you can grade based on whether specific conditions are met. The following example is a grading code for a practice where students are asked to write a haiku using the print() function three times.

After importing the file written by the student (main.py), the grading is based on whether the print() function is written three times.

import os
import subprocess
import sys
from testcases import *
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 at least 3 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 have created a beautiful haiku using the print() function! \n')
    else:
        elice_utils.secure_send_grader('❌ Incorrect! Try creating a haiku using the print() function three times.\n')

    # Sending the final score
    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)

Last updated