RestrictedStudentAnswers

From WeBWorK
Jump to: navigation, search

Restricting Allowed Student Answer Formats: PG Code Snippet


This PG code shows how to check student answers that are equations. Note that this is an insertion, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

In general, there are a number of different restrictions that one might want to place on a student answer: that it be completely simplified, that it be exact, etc. In the following we consider a number of these. Note that there are often many ways to accomplish the same goals, and the method suggested here may or may not be directly applicable to the case you are considering. Often using a Custom Answer Checker or setting a Custom Error Message will also be desirable.

Examples below:

Problem Techniques Index

Requiring that an answer be simplified to a constant

PG problem file Explanation
Context("Numeric");

$expr = Formula("sin(x)^2 + cos(x)^2");
$deriv = Compute(0);

Setup: The set up section has nothing out of the ordinary. Our variable $deriv is the derivative of $expr, which is of course zero.

BEGIN_TEXT
Find and simplify completely:
$BR
\(\frac{d}{dx}\left($expr\right) = \)
\{ ans_rule(25) \}
END_TEXT

Main Text: The problem text section of the file is as we'd expect.

$showPartialCorrectAnswers = 1;

ANS( $deriv->cmp(showTypeWarnings=>0) );

ENDDOCUMENT();

Answer Evaluation: Here we've turned off type warnings in the answer checking, so that a student entering an un-simplified answer (e.g., 2 sin(x) cos(x) + 2 cos(x) (-sin(x))) will have it marked wrong (but not get feedback that says "you should have entered a number").

top of page

Requiring that an answer be a simplified fraction

PG problem file Explanation
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"contextFraction.pl",
);
TEXT(beginproblem());

Initialization: We need to include the macro file contextFraction.pl.

Context("Fraction-NoDecimals");
Context()->operators->undefine('+','-','*',
                               '*','**','^');

$a = random(2,4,1);
$b = random(1,9,1);
$c = random(1,9,1);
$den = $c + $a*$a;
$frac = Compute("$b/$den");

Setup: Here we specify that we are using the Fractions-NoDecimals Context, which requires that answers be fractions and not decimals. To ensure that students do the simplification rather than typing the answer without expanding it, we undefine operators other than division (see Restricting Functions and Operators).

Note that because we've undefined these operators for all MathObjects, we can't define the answer as $frac=Compute("$b/($c + $a^2)");. The operators + and ^ are undefined, so we don't have them available. In this case we do the calculation of the denominator using Perl first, and then use the MathObject to create the answer.

Also note that by default a Fraction will be reduced to lowest terms.

BEGIN_TEXT
Find and simplify completely the value of
\(f($a)\) if
\[ f(x) = \frac{$b}{$c + x^2}. \]
$BR
\(f($a) = \)
\{ ans_rule(25) \}
$BR
${BITALIC}(Simplify your answer as much as
possible, and enter a fraction instead of
a decimal.)$EITALIC
END_TEXT

Main Text: The problem text section of the file is as we'd expect.

$showPartialCorrectAnswers = 1;

ANS( $frac->cmp(studentsMustReduceFractions=>1,
                strictFractions=>1,strictMinus=>1,
                strictMultiplication=>1) );

ENDDOCUMENT();

Answer Evaluation: In the answer evaluation, we require students to reduce fractions, and by setting the remaining three flags, that the fraction be an integer over another integer.


Problem Techniques Index

follow us