Difference between revisions of "CustomAnswerCheckers"
Line 2: | Line 2: | ||
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> |
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> |
||
− | <em>This |
+ | <em>This is the essential PG code to write problems that check "arbitrary" conditions on the student's answer. |
− | + | <br /> |
|
+ | <br /> |
||
You may also be interested in [http://webwork.maa.org/wiki/PeriodicAnswers PeriodicAnswers] |
You may also be interested in [http://webwork.maa.org/wiki/PeriodicAnswers PeriodicAnswers] |
||
</em> |
</em> |
||
Line 10: | Line 10: | ||
<p style="text-align:center;"> |
<p style="text-align:center;"> |
||
[[IndexOfProblemTechniques|Problem Techniques Index]] |
[[IndexOfProblemTechniques|Problem Techniques Index]] |
||
− | </p> |
||
− | |||
− | <p> |
||
− | 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. |
||
</p> |
</p> |
||
Line 34: | Line 30: | ||
<td style="background-color:#ccffcc;padding:7px;"> |
<td style="background-color:#ccffcc;padding:7px;"> |
||
<p> |
<p> |
||
− | <b>Initialization:</b> Standard. |
||
+ | <b>Initialization:</b> Standard. You might want to use [http://webwork.maa.org/pod/pg_TRUNK/macros/answerCustom.pl.html answerCustom.pl.html], although it is not necessary. |
||
</p> |
</p> |
||
</td> |
</td> |
||
Line 41: | Line 37: | ||
<td style="background-color:#ffffdd;border:black 1px dashed;"> |
<td style="background-color:#ffffdd;border:black 1px dashed;"> |
||
<pre> |
<pre> |
||
+ | Context("Numeric"); |
||
+ | |||
$ans = pi/3; |
$ans = pi/3; |
||
$val = cos($ans); |
$val = cos($ans); |
||
Line 61: | Line 59: | ||
<td style="background-color:#ffdddd;border:black 1px dashed;"> |
<td style="background-color:#ffdddd;border:black 1px dashed;"> |
||
<pre> |
<pre> |
||
+ | Context()->texStrings; |
||
BEGIN_TEXT |
BEGIN_TEXT |
||
Enter a value of \(x\) for which \(\cos(x) = $val\): |
Enter a value of \(x\) for which \(\cos(x) = $val\): |
||
\(x = \) \{ ans_rule(25) \} |
\(x = \) \{ ans_rule(25) \} |
||
END_TEXT |
END_TEXT |
||
+ | Context()->normalStrings; |
||
</pre> |
</pre> |
||
<td style="background-color:#ffcccc;padding:7px;"> |
<td style="background-color:#ffcccc;padding:7px;"> |
||
Line 80: | Line 80: | ||
return cos($correct) == cos($student); |
return cos($correct) == cos($student); |
||
} ) ); |
} ) ); |
||
+ | |||
+ | ENDDOCUMENT(); |
||
</pre> |
</pre> |
||
<td style="background-color:#eeccff;padding:7px;"> |
<td style="background-color:#eeccff;padding:7px;"> |
Revision as of 15:27, 6 November 2010
Custom Answer Checkers: PG Code Snippet
This is the essential PG code to write problems that check "arbitrary" conditions on the student's answer.
You may also be interested in PeriodicAnswers
PG problem file | Explanation |
---|---|
DOCUMENT(); loadMacros( "PGstandard.pl", "MathObjects.pl", ); TEXT(beginproblem()); |
Initialization: Standard. You might want to use answerCustom.pl.html, although it is not necessary. |
Context("Numeric"); $ans = pi/3; $val = cos($ans); |
Setup: 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
Similarly, |
Context()->texStrings; BEGIN_TEXT Enter a value of \(x\) for which \(\cos(x) = $val\): \(x = \) \{ ans_rule(25) \} END_TEXT Context()->normalStrings; |
Main 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); } ) ); ENDDOCUMENT(); |
Answer Evaluation: 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 sub mycheck { my ($correct, $student, $ansHash) = @_; return cos($student) == cos($correct); } ANS( $ans->cmp( checker=>~~&mycheck ) ); (The
One final point: we can set an error message in the answer checker by using sub mycheck { my ($correct, $student, $ansHash) = @_; Value->Error("Try again") if cos($student)==sqrt(3)/2; return cos($student) == cos($correct); } ANS( $ans->cmp( checker=>~~&mycheck ) ); ENDDOCUMENT(); |
- POD documentation: answerCustom.pl.html
- PG macro: answerCustom.pl