Custom Grading for Interactive Exercises

Implementing Automated Feedback for R and Python Coding Challenges

Discover custom grading techniques for interactive coding exercises in Quarto Live. Learn how to automatically evaluate R and Python exercises, provide hints, and deliver immediate feedback using custom grading algorithms.

Tools
Author
Affiliation
Published

March 21, 2025

Keywords

interactive grading R, interactive grading Python, Quarto grading

Introduction

Interactive exercises are a powerful way to help learners practice coding and receive immediate feedback. By implementing custom grading techniques in your interactive documents, you can automatically evaluate learner submissions, provide helpful hints, and offer detailed feedback. This tutorial explains how to set up custom grading for both R and Python exercises within Quarto Live.



1. Why Custom Grading?

Custom grading allows you to:

  • Automate Feedback: Provide instant responses to learner submissions.
  • Guide Learning: Offer hints and detailed explanations for common mistakes.
  • Enhance Engagement: Keep learners motivated with immediate, targeted feedback.

2. Custom Grading in R

In R, you can assign a unique exercise label to a code block and then attach a grading block that evaluates the learner’s code.

Example: Sum Exercise in R

Exercise Code

```{webr}
#| exercise: ex_sum
# Complete the expression to compute the sum of 1 + 2 + 3 + 4.
1 + 2 + 3 + ______
```

Grading Code

```{webr}
#| exercise: ex_sum
#| check: true
expected <- 10
if (identical(.result, expected)) {
  list(correct = TRUE, message = "Great job! Your sum is correct.")
} else {
  list(correct = FALSE, message = "Incorrect. Ensure that the expression sums to 10 by filling in the blank with the correct number.")
}
```
Note

In this exercise, the learner should fill in the blank with the number 4 to produce the sum of 10.*

Complete Example

Sources
```{webr}
#| exercise: ex_sum
# Complete the expression to compute the sum of 1 + 2 + 3 + 4.
1 + 2 + 3 + ______
```

```{webr}
#| exercise: ex_sum
#| check: true
expected <- 10
if (identical(.result, expected)) {
  list(correct = TRUE, message = "Great job! Your sum is correct.")
} else {
  list(correct = FALSE, message = "Incorrect. Ensure that the expression sums to 10 by filling in the blank with the correct number.")
}
```

3. Custom Grading in Python

Similarly, for Python exercises, you can create a code block with a blank for the learner to complete, and then use a grading block that checks the output.

Example: Sum Exercise in Python

Exercise Code

```{pyodide}
#| exercise: ex_sum_py
# Complete the expression to compute the sum of 1 + 2 + 3 + 4.
result = 1 + 2 + 3 + ______
print("The sum is", result)
result
```

Grading Code

```{pyodide}
#| exercise: ex_sum_py
#| check: true
expected = 10
if result == expected:
    feedback = {"correct": True, "message": "Correct! The sum is 10."}
else:
    feedback = {"correct": False, "message": "Incorrect. Please ensure your expression results in 10 by filling in the blank appropriately."}
feedback
```
Note

In this exercise, the learner should fill in the blank with the number 4 to produce the sum of 10.*

Complete Example

Sources
```{pyodide}
#| exercise: ex_sum_py
# Complete the expression to compute the sum of 1 + 2 + 3 + 4.
result = 1 + 2 + 3 + ______
print("The sum is", result)
result
```

```{pyodide}
#| exercise: ex_sum_py
#| check: true
expected = 10
if result == expected:
    feedback = {"correct": True, "message": "Correct! The sum is 10."}
else:
    feedback = {"correct": False, "message": "Incorrect. Please ensure your expression results in 10 by filling in the blank appropriately."}
feedback
```

4. Best Practices for Custom Grading

  • Clear Instructions:
    Provide precise instructions in the exercise block so that learners know what is expected.

  • Meaningful Hints:
    Use hint blocks to guide learners toward the correct solution without giving it away.

  • Detailed Feedback:
    Ensure that the grading block checks for common errors and provides actionable feedback.

  • Consistent Exercise Labels:
    Use unique exercise labels (e.g., ex_sum or ex_sum_py) to link the exercise, hint, and grading blocks effectively.

5. Further Reading

Conclusion

Custom grading for interactive exercises allows you to provide immediate, personalized feedback to learners. By following these examples in both R and Python, you can implement automated grading that guides learners to the correct solution while enhancing their overall learning experience. Experiment with different approaches and refine your grading algorithms to best support your learners.

Back to top

Reuse

Citation

BibTeX citation:
@online{kassambara2025,
  author = {Kassambara, Alboukadel},
  title = {Custom {Grading} for {Interactive} {Exercises}},
  date = {2025-03-21},
  url = {https://www.datanovia.com/learn/interactive/advanced/grading.html},
  langid = {en}
}
For attribution, please cite this work as:
Kassambara, Alboukadel. 2025. “Custom Grading for Interactive Exercises.” March 21, 2025. https://www.datanovia.com/learn/interactive/advanced/grading.html.