The “Code Widget” is the most powerful assessment tool as it allows you to make coding challenges for learners. This way, you can test their coding skills, which is an important part of many courses.

Edit mode

In such questions, the learners must write a function. To test the function, you will provide inputs and corresponding outputs in separate text files. These will be your test cases. If the function is written correctly, the user’s outputs will match the ones you have specified in your output file.

Selecting “Code Widget” from “Question Type” will create a template for you as below:

Draft of Code Widget question
Draft of Code Widget question

Question Details

Add a detailed description of the coding challenge here. The learner should be clear about the problem statement. It is good practice to mention a sample input and the corresponding output.

Evaluation Input File

All your test inputs for the user’s function must be listed in a .txt file. This file is added to the assessment assets.

Click on the assessment title on the top left of your current page. This will take you to the assessment editor page, where you can add asset files.

Come back to the page where you were creating your question, and select the input file from the drop-down menu.

Evaluation Output File

All the outputs for your test inputs must be stored in a .txt file as well. For the sake of ease, it is recommended that the outputs are in the same order as the inputs. This file must also be uploaded as an assessment asset.

Come back to the page where you were creating your question, and select the output file from the drop-down menu. Select the file from the drop-down menu.

Click “Save” in the top right corner to save the input and output files in the question; otherwise, the assessment won’t run.

Coding window

In the coding window, the learner must write their code for the given task. It is recommended that you provide an empty function for them to write code in.

Let’s say you want to test a function called square that takes in a number and returns its square. The coding window would look something like this:

def square(n):
# Write your code here
pass

The pass is just there to make the empty function run for evaluation. It can be removed once the user has written the code.

Evaluation

You will write your evaluation code in this tab. The evaluation code generally follows these steps:

  1. Read the input and output files.
  2. Call the learner’s function with the inputs.
  3. Print the output with each input (if the user is already printing the output, you can skip this).

We’ll look at this in more detail shortly.

Prepend

Any prepended code that you do not want to show to the learner goes into this tab. For example, you may want to import certain libraries or write a custom implementation for a data structure that the user can access.

Hints

You can use the Hints tab to add instructional hints for the user.

We’ll understand all of these in more depth with the following example.

Example - Find the maximum number in a list

In this example, the learner must write a function that takes in a Python list and returns the maximum number from it.

  • Begin by writing a description of the task in the “Question Detail” as below:
Example: Problem statement
Example: Problem statement
  • Create a file, input.txt, locally with the following data in it.

    20,40,12,82
    45,17,90,102,30
    100,52,35,0
    

    For evaluation, each line will be converted to a list and passed into the user’s function.

  • Create a file, output.txt, locally with the following data in it.

    82
    102
    100
    

    It should have the expected output for each list being tested.

  • Upload both files in the assets and select them from the drop-down in the widget as described above.

  • In the “Coding Window”, we’ll give the learner an empty find_max() function to implement:

    def find_max(num_list):
      # Write your code here
      pass
    
  • EvaluateCodeEvalAssessment the learner’s code in the “Evaluation” tab of the Code widget.

  • You can add the hint(s) to help learners in case they are stuck on a coding challenge. To add a hint, go to the “Hints” tab in the Code widget and click on “Add Hint”:

Here’s how the question will look in the edit mode:

Sample coding question
Sample coding question

Published mode

The coding question is displayed to the learner like this:

Learner's view
Learner's view

Learners can click “Test” to verify how many test cases are passed. Note that details of the test cases won’t be visible to learners.

Grading

A mark is awarded for each test case passed in the Code Widget. Hence, it is possible for users to earn partial marks in a coding question.