## WeBWorK Problems

### Answer conditional on PopUp response

by Eric Errthum -
Number of replies: 1
I'm trying to write a problem where the student gets to choose what type of function they want to use and then model the data with that type of function. Below is my attempt (with a simpler version of what I'm trying to do). It compiles fine, but I can't get it to compare to the correct function. It's as if the conditional ($stud_ans==$exp) is always returning true. I also tried using MultiAnswer but there seems to be inherent problems between it and the PopUp. Any help would be greatly appreciated!

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

DOCUMENT();

"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");$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([$lin,$exp])->cmp);
$ans_hash1 =$inputs_ref->{ANS_NUM_TO_NAME(1)};
$stud_ans =$ans_hash1->{student_ans};
if ($stud_ans==$exp) {$funans=$expans;} else {$funans=$linans;};
ANS( $funans->cmp()); ############################################## ENDDOCUMENT(); In reply to Eric Errthum ### Re: Answer conditional on PopUp response by Eric Errthum - 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();