String or Other Type

Answer could be a string or another data type

Complete Code

Download file: StringOrOtherType.pg

PG problem file

Explanation

DOCUMENT();

loadMacros('PGstandard.pl', 'PGML.pl', 'PGcourse.pl');

Preamble

These standard macros need to be loaded.
$y = random(0, 4);

if ($y < 4) {
    $answer = String('none')->cmp(typeMatch => Formula('x'));
} else {
    $answer = Formula('2*x')->cmp(typeMatch => Formula('x'));
}

Setup

There are several predefined strings, such as NONE, DNE, INF, INFINITY. If you need another string added to the context, see Adding Strings to a Context.

When $answer = Formula('2x') 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.

BEGIN_PGML
Is there a line through the points [`(0, 0)`], [`(1, 2)`], and [`(2, [$y])`]?
If there is, enter the equation for this line.  If not, enter _NONE_.

[`y =`] [_]{$answer}{15}
END_PGML

Statement

This is the problem statement in PGML.
BEGIN_PGML_SOLUTION
Solution explanation goes here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.