MultiAnswerProblems
Multi-Part Problems, Linked Answer Blanks: PG Code Snippet
This code snippet shows the essential PG code to create a problem with multiple answer blanks which use the answers to all of the answer blanks to decide if the problem is correct. 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.
PG problem file | Explanation |
---|---|
loadMacros("parserMultiAnswer.pl"); |
In the initialization section of the file, load |
$fac1 = Formula("(1 - x)"); $fac2 = Formula("(1 + x)"); $multipart = MultiAnswer($fac1, $fac2)->with( singleResult => 0, checker => sub { my ( $correct, $student, $self ) = @_; my ( $f1stu, $f2stu ) = @{$student}; my ( $f1, $f2 ) = @{$correct}; if ( ($f1 == $f1stu && $f2 == $f2stu) || ($f1 == $f2stu && $f2 == $f1stu) ) { return [1,1]; } else { if ($f1 == $f1stu || $f2 == $f1stu) { return [1,0]; } elsif ($f1 == $f1stu || $f2 == $f2stu) { return [0,1]; } else { return [0,0]; } } } ); |
In the problem set-up section of the file we define a MultiAnswer object that knows how to deal with the problem. Here we define an object that will take two answers and check that they are correct (in either order).
First, the
Then, the
The checker routine then returns a reference to a list of results for the problem. In this case there are two answer blanks, so there are two return values. All return values should be 0 or 1, according to whether the answer for that answer blank is correct or not. Note that if we made this an "all or nothing" problem (that is, we set It is possible to set an answer message that will be displayed when the problem is checked, too. For example, if we wanted to set a message when one of the parts was wrong, we could replace the section of the checker code that deals with incorrect answers with: if ($f1 == $f1stu || $f2 == $f1stu) { $self->setMessage(1,"This is correct."); $self->setMessage(2,"Check your answer by using FOIL."); return [1,0]; } elsif ($f1 == $f1stu || $f2 == $f2stu) { $self->setMessage(1,"Check your answer by using FOIL."); $self->setMessage(2,"This is correct."); return [0,1]; } else { return [0,0]; } |
BEGIN_TEXT Factor: \(1-x^2 = \) \{$multipart->ans_rule(10)\} \{$multipart->ans_rule(10)\} END_TEXT |
In the text section of the problem we proceed as expected, but define the answer blanks using the MultiAnswer object that we defined in the initialization section of the problem. |
ANS( $multipart->cmp() ); |
And the answer and solution section of the file is straightforward. |