StringOrOtherType1

From WeBWorK
(Difference between revisions)
Jump to: navigation, search
m
(PGML example link)
 
(One intermediate revision by one user not shown)
Line 5: Line 5:
 
This PG code shows how to prevent error messages from appearing when an answer may be one of several data types.
 
This PG code shows how to prevent error messages from appearing when an answer may be one of several data types.
 
</p>
 
</p>
* Download file: [[File:StringOrOtherType1.txt]] (change the file extension from txt to pg when you save it)
+
* File location in OPL: [https://github.com/openwebwork/webwork-open-problem-library/blob/master/OpenProblemLibrary/FortLewis/Authoring/Templates/Precalc/StringOrOtherType1.pg FortLewis/Authoring/Templates/Precalc/StringOrOtherType1.pg]
* File location in NPL: <code>FortLewis/Authoring/Templates/Precalc/StringOrOtherType1.pg</code>
+
* PGML location in OPL: [https://github.com/openwebwork/webwork-open-problem-library/blob/master/OpenProblemLibrary/FortLewis/Authoring/Templates/Precalc/StringOrOtherType1_PGML.pg FortLewis/Authoring/Templates/Precalc/StringOrOtherType1_PGML.pg]
  
 
<br clear="all" />
 
<br clear="all" />

Latest revision as of 20:13, 13 June 2015

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

follow us