WeBWorK Problems

Answer conditional on PopUp response

Re: Answer conditional on PopUp response

by Eric Errthum -
Number of replies: 0
For what it's worth, the mistake was in the comparison ==. For strings, use eq. So a fully functional example is below.

----------------------------------------------------------

DOCUMENT();

loadMacros(
"PGstandard.pl",
"parserPopUp.pl",
"parserOneOf.pl",
);
TEXT(beginproblem());

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

$a=random(2,9,1);
$b=2**$a;
$m=(2**$a-1)/$a;

$linans = Formula("$m*x+1");
$expans=Formula("2**x");
$noans=OneOf($linans, $expans);

$lin="Linear";
$exp="Exponential";
@choices = ("Select",$lin,$exp);
$pop = PopUp([@choices], $choices[0]);
Context($pop->context);

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

BEGIN_TEXT

A function \(y=f(x)\) goes through the points \((0,1)\) and \(($a,$b)\). $PAR

The best type of function to model the data is: \{$pop->menu\} $PAR

Doing so gives \(y=\) \{ans_rule(20)\}. $PAR

END_TEXT

Context()->normalStrings;

ANS(OneOf(@choices[1,2])->cmp());
$ans_hash1 =$inputs_ref->{ANS_NUM_TO_NAME(1)};
if ($ans_hash1 eq $lin) {ANS( $linans->cmp())}
elsif ($ans_hash1 eq $exp) {ANS( $expans->cmp())}
else {ANS($noans->cmp())}


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


ENDDOCUMENT();