## PREP 2013 Question Authoring - Archived

by Ravinder Kumar -
Number of replies: 3
In multiple choice format, correct answer to a question is a conditional statement

if ( $a > 0 ) { "increasing, concave up"; } else { "decreasing, concave down"; } So the string for the correct answer depends upon the value of$a. Should we create a function that returns the string or directly incorporate this code for the correct answer? I am getting an error on directly replacing the code for the correct answer  in place of  "   "  in $mc->qa( " ", " ") If we have to create a function that returns a string, what is the code for creating function in PERL? In reply to Ravinder Kumar ### Re: Conditional answer by Ravinder Kumar - Figured out Have to use sub (function). Here is the code sub correct_stg { if ($a > 0 ) {
"increasing, concave up";
} else {
"decreasing, concave up";
}

by Paul Pearson -
Hi Ravinder,

Thanks for posting this to the forum! In general, I would like to encourage everyone who posts a code question to the forum to include all of their source code, not just snippets, since that makes it easier to understand the context and also look for other things that might be improved.

I think you may be able to assign the correct answer to a scalar $correct if ($a > 0 ) {
$correct = "increasing, concave up"; } else {$correct = "decreasing, concave down";
}

and then use that scalar in the multiple choice constructor

$mc->qa( "What is the behavior of the function?",$correct);

Of course, you can name the scalar \$correct whatever you want.

Best regards,

Paul Pearson