With this evaluation type, you can type your own Python code in the Definition field to evaluate the student’s answer. The solution/feedback rule will be activated only if the Python code prints
True. If you want to use Maxima code instead of Python code, you can use the evaluation type eval statement. If you want to use R instead of Python, you can use the evaluation type eval R.
Note that this evaluation type has nothing to do with the exercise type Python. In the exercise type Python, the student should write Python code in the answer field, whereas this article is just about writing solution or feedback rules using Python.
The student’s answer can be accessed in two ways.
answerfor the student’s answer in the answer field that belongs to the solution/feedback rule. The
answervariable will be parsed using Sympy’s parse_maxima function.
answersfor a list of student answers of all answer fields. The values in this list will not be parsed, and thus will be in Maxima syntax. The list starts with index 0. In case an answer value is empty, the list entry for this answer field will be
In your Python code, you can write your own functions, but you can also import functions from one of the available Python modules. All available Python modules can be found in the authoring manual.
❗ Note that this evaluation type does not have automated feedback.
Using function variables in your Python code
Let’s say we want to define the function f(x)=3*x+5 in our Python code. To do this, we need to make sure that the letter x is seen as a function variable and not a Python variable in the code. This can be done by using the function
symbols from the Python package Sympy. This looks as follows:
More information and documentation on the symbols function can be found here.
Example: the following code checks if the student’s answer is 2x.
Note that we could also check if the student’s answer is 2x by using the evaluation type eval normal.
More on evaluation types