Solve the following quadratic equation

x^2=a^2

I'd like the students to answer with

x=a,x=-a

I'd like to customize the error messages so that when students enter answers like the following they are given the corresponding error messages (and combinations thereof)

a |
Your first solution is correct, but you need to write x=____ Are you sure you have all the solutions? |

a,-a |
Your first solution is correct, but you need to write x=____ Your second solution is correct, but you need to write x=____ |

x=a |
Are you sure you have all of the solutions? |

I have made some progress following http://webwork.maa.org/wiki/Custom_Answer_Checkers_for_Lists but I have some issues outstanding:

- most importantly, my code doesn't check the correctness (!)
- more subtly, when a student enters x=a the error message persists as if there are
*two*solutions. I would quite like to have the error message 'Are you sure you have all the solutions?' as above

I hope these aren't too elementary, I'm quite new to WeBWorK- if there's an existing or better way to do this, I'm very open to it! Thanks in advance.

Here's a complete MWE comprising what I have so far:

DOCUMENT();

loadMacros(

"PGstandard.pl",

"MathObjects.pl",

"parserAssignment.pl",

"PGML.pl",

"answerHints.pl",

);

##############################################

Context("Numeric");

# the variable of the equation

$var = 'x';

Context()->variables->are($var=>'Real');

# custom error messages

parser::Assignment->Allow;

Context()->flags->set(reduceConstantFunctions=>0,formatStudentAnswer=>parsed);

Context()->{error}{msg}{"The left side of an assignment must be a variable or function"}

= "Your answer should be in the form $var = ___";

Context()->{error}{msg}{"The right side of an assignment must not include the variable being defined"}

= "The right side must not include the variable being defined";

# add solution strings to context- this means that if

# students enter these (and they are not correct), then

# WW will not give a Context warning

Context()->strings->add("no solution"=>{},

"no solutions"=>{alias=>'no solution'},

"none"=>{alias=>'no solution'},

);

# the right hand side

$a = random(2,10,1);

# answer

$ans = $a;

$ans = Compute($ans);

$ans1 = -$a;

$ans1 = Compute($ans1);

#$ansEq = List($a,$b);

#$ansEq=Formula("$var=$ans");

#$ansEq=List(Formula("$var=$ans"),Formula("$var=$ans1"));

$ansEq=Formula("$var=$ans","$var=$ans1");

##############################################

TEXT(beginproblem());

BEGIN_PGML

Solve the following quadratic equation

[`

[$var]^2 = [$a**2]

`]

[____________]

* Enter multiple answers separated by commas, such as [`x=1,x=-1`]

* If you need to use the square root symbol,

as in [`x=\sqrt{17}`], type it using: *sqrt(17)*

* If there are no solutions, enter *no solutions*

END_PGML

#ANS(Formula("$var=$ans,$var=$ans1")->cmp);

## answer checker for lists tutorial:

##

## http://webwork.maa.org/wiki/Custom_Answer_Checkers_for_Lists

##

##

#ANS(Formula("$var=$ans,$var=$ans1")->cmp);

ANS(Formula("$var=$ans,$var=$ans1")->cmp(list_checker => sub {

my ($correct,$student,$ansHash,$value) = @_;

my $n = scalar(@$student); # number of student answers

my $score = 0; # number of correct student answers

my @errors = (); # stores error messages

my $i, $j; # loop counters

#

# Loop though the student answers

##

for ($i = 0; $i < $n; $i++) {

my $ith = Value::List->NameForNumber($i+1);

my $p = $student->[$i]; # i-th student answer

#

# Check that the student's answer is a point

#

if ($p->type ne "Assignment") {

push(@errors,"Your $ith entry needs to be in the form x=____");

next;

}

#

# Check that the point hasn't been given before

#

for ($j = 0, $used = 0; $j < $i; $j++) {

if ($student->[$j]->type eq "Assignment" && $student->[$j] == $p) {

push(@errors,"Your $ith solution is the same as a previous one") unless $ansHash->{isPreview};

$used = 1; last;

}

}

#

# If not already used, check that it satisfies the equation

# and increase the score if so.

#

if (!$used) {

$score++;

#my ($a,$b) = $p->value;

#if ($a + $b == 5) {$score++} else {

# push(@errors,"Your $ith point is not correct") unless $ansHash->{isPreview}

#}

}

}

#

# Check that there are the right number of answers

#

if (!$ansHash->{isPreview}) {

push(@errors,"Are you sure you have all the solutions?") if $n < 2;

push(@errors,"You have given too many solutions- every quadratic equation has at most two solutions") if $n > 2;

}

return ($score,@errors);

}));

BEGIN_PGML_SOLUTION

The answer is [`[$ansEq]`].

END_PGML_SOLUTION

##############################################

ENDDOCUMENT();