# Difference between revisions of "CustomAnswerCheckers"

## Custom Answer Checkers: PG Code Snippet

This code snippet shows the essential PG code to write problems that check "arbitrary" conditions on the student's answer. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

Note that it is possible to do this with old-style answer checkers as well. However, it's more straightforward to do it with MathObjects based answer checkers, so that's the method that we use here.

PG problem file Explanation
  $ans = pi/3;$val = cos($ans);  To set up the custom answer checker we will override the answer checker routine for the MathObject that we're using to check the answer. Thus our answer object should be of the same type (e.g., Real, Formula, etc.) as what we want the student to be entering. For example, here we're going to ask for a value of x such that cos(x)=cos($ans). Thus we set up the answer to be a real number.

In this sample, we've taken advantage of a bunch of overloading that MathObjects do: the line $ans = pi/3 is doing the same thing as $ans = Real("pi/3") or $ans = Compute("pi/3"), because pi is already defined as a Real, and the division / is overloaded for MathObjects. Similarly, $val = cos($ans) takes advantage of the cos() function being overloaded to produce a MathObject when the argument is a MathObject. Thus this is the same as $val = Compute("cos(x)")->eval(x=>$ans) (which would be necessary if $ans wasn't already a MathObject).

  BEGIN_TEXT
Enter a value of $$x$$ for which $$\cos(x) = val$$:
$$x =$$ \{ ans_rule(25) \}
END_TEXT


We don't have to make any changes or additions to the text section of the file.

  ANS( $ans->cmp( checker=>sub { my ($correct, $student,$ansHash ) = @_;
return cos($correct) == cos($student);
} ) );


Then when setting up the answer and solution section, we overwride the default answer checker in the answer. The replacement is a Perl subroutine that takes as its arguments the correct answer, student answer, and answer hash that is being processed in the answer comparison. Its return value should be 1 if the student's answer is correct, and 0 (false) otherwise.

We could also specify the answer checker as a separate subroutine by writing ANS( $ans->cmp( checker=>~~&mycheck ) ) and then including mycheck as a separate subroutine, viz., sub mycheck { my ($correct, $student,$ansHash) = @_;
return cos($student) == cos($correct);
}


(The ~~ in the above is remapped to a backslash in the course of the PG translation of the problem.)

One final point: we can set an error message in the answer checker by including the line Value->Error("message") after the error. This will set the message that is displayed to the student and exit the checker with an incorrect return value.