case2. Check Specific Conditions

After loading the code file submitted by the student, you can evaluate the submission based on specific conditions. The example below is a grading code for a practice that requires creating a haiku using the print() function three times.

After loading the file written by the student (main.py), the code checks if the print() function is used three times to grade the practice.

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 three times or more
    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 using the print() function three times to create a haiku.\n')

    # Sending the final practice 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 while grading. Please check if your code runs correctly.\n')
    elice_utils.secure_send_score(0)
    sys.exit(1)

Last updated