StringOrOtherType1

From WeBWorK_wiki
Revision as of 22:33, 15 June 2013 by Paultpearson (talk | contribs)
Jump to navigation Jump to search

Answer is a String, or a Number, or a Function, etc.

Click to enlarge

This PG code shows how to prevent error messages from appearing when an answer may be one of several data types.


Templates by Subject Area

PG problem file Explanation

Problem tagging data

Problem tagging:

DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"AnswerFormatHelp.pl",
);

TEXT(beginproblem());

Initialization:

Context("Numeric");

$y = random(0,4,1);

if ($y < 4) {
   $answer = String("none");
} else {
   $answer = Formula("2*x");
}

Setup: There are several predefined strings, such as NONE, DNE, INF, INFINITY. If you need another string added to the context, see strings in context.

Context()->texStrings;
BEGIN_TEXT
Is there a line through the points \( (0,0) \), 
\( (1,2) \), and \( (2,$y) \)?  If there is, 
the equation for this line.  If not, enter
${BITALIC}NONE${EITALIC}.
$BR
$BR
\( y = \) \{ ans_rule(20) \}
\{ AnswerFormatHelp("formulas") \}
END_TEXT
Context()->normalStrings;

Main Text:

$showPartialCorrectAnswers = 1;

ANS( $answer->cmp( typeMatch=>Formula("x") ) );

Answer Evaluation: When $answer = Formula("2*x") and a student enters the string NONE, they will not get any error message because when the answer checker expects a formula and gets a string it is set up not to balk. However, when $answer = String("none") and a student enters the formula 2x, they will get an error message. This is because the answer checker is expecting a string and gets a formula, and when this happens it balks. We must use typeMatch=>Formula("x") so that in the event the answer is a string, no error message will appear.

Context()->texStrings;
BEGIN_SOLUTION
${PAR}SOLUTION:${PAR}
Solution explanation goes here.
END_SOLUTION
Context()->normalStrings;

COMMENT('MathObject version.');

ENDDOCUMENT();

Solution:

Templates by Subject Area