# Perform Complex Operations in Answer Keys

When you create multi-part or multi-mode questions, especially answer-dependent questions, you sometimes need to determine answer keys for later parts of the question based on your student's responses to earlier parts of the question. But, your answer key must be specified on a single line in Answer. To perform complex or repetitive parsing or computations for answer keys, you can use the postcalc() function.

The postcalc() function lets you define a variable in Question that holds the code that you want to evaluate in Answer. You can pass values to your code, and postcalc() returns the result of the last statement in your code. This helps you accomplish two things:

• You can define a complex, multi-line algorithm that is evaluated to define an answer key based on your student's responses to other parts of the question.
• You can reuse a formula with different values for multiple lines in your answer key.
1. Open your question in the Question Editor.
2. Add the following code at the beginning of Question:
<eqn include('/userimages/feedback/physlabs1.pl')>

This code is required in order to use the postcalc() function.

3. In Question, define a variable that contains the code that you want to be evaluated in your answer key. For example:
<eqn>
$mycalc='decform($a**2 + $b**2,3)'; '' </eqn> Your code can include multiple statements, and you can format it using multiple lines. Values that are passed to your code from the postcalc() function are assigned to the special variables $_[0], $_[1], and so on. Important • Be sure to escape single or double quotation marks in your code that would end the variable definition. You can do this by preceding the quotation mark with a backslash as in the following example: $mycalc='$a=\'test\';$b=3*$firstresult'; • Do not use the postcalc() function in your code. 4. In Answer, add the postcalc() function where your code should be evaluated. For example: <EQN postcalc($mycalc)>
To pass values to your code, list them after the variable that contains your code. For example:
<EQN postcalc($mycalc,6)> The postcalc() function returns the result of the last statement in your code, so you can use it for variable assignment. For example: <EQN$mychoices[2] = postcalc($mycalc,6)> 5. Click Test/Preview to test the appearance and behavior of the question. See Test Questions. 6. When your question displays and functions correctly, click Save. ## Example Question Using postcalc() to Calculate the Answer Key The following table summarizes an actual question.  QID 1589522 Template2 3.POSTC.01 Numerical  %total_feedback = (low => "Count more cells for an accurate sample."); %ID_feedback = (low => "Check your identification.", high => "Check your identification." );$calc_range = '$LINK=\'box_total: no_icon\'; decform(userinput(max(0,$total*($_[0]-0.05)),$total*($_[0]+0.05),$total*$_[0], \%ID_feedback),0)'; '' View the slide of an onion root tip and count the cells in each phase of cell division. TOTAL<_> Interphase<_> Prophase<_> Metaphase<_> Anaphase<_> Telophase<_>   {tab} 0 {tab} 0 {tab} 0 {tab} 0 {tab} 0 {tab} 0 ## Example Question Using postcalc() to Parse Responses The following table summarizes a question that uses postcalc() to determine characteristics of the student's triangle based on the lengths of its sides.  QID 1589514 Template2 3.POSTC.02 Multi-Mode...NS  @clues=(' ', 'Are all three sides equal?', 'Are two sides equal?', 'Are any sides equal?', 'Is there a right angle?', 'Is there an obtuse angle?', 'Are all the angles acute?' ); @reinforcement=(' ', 'A triangle with three equal sides is equilateral.', 'A triangle with two equal sides is isosceles.', 'A triangle with three different sides is scalene.', 'When one angle is 90°, it is a right triangle.', 'When one angle is greater than 90°, it is obtuse.', 'When all angles are less than 90°, it is acute.' );$triangle=' @squares = sort {$a <=>$b} ($AB**2,$AC**2,$BC**2); if (@squares[0] == @squares[2]) {@choices=(1,2)} elsif (@squares[0] == @squares[1]) {@choices=(2)} elsif (@squares[1] == @squares[2]) {@choices=(2)} elsif ((@squares[0] != @squares[1]) && (@squares[0] != @squares[2]) && (@squares[1] != @squares[2])) {@choices=(3)} if (abs(@squares[2] - @squares[1] - @squares[0]) < @squares[2] * 0.05) {@choices=(@choices,4)} elsif (@squares[2] > @squares[0] + @squares[1]) {@choices=(@choices,5)} elsif (@squares[2] < @squares[0] + @squares[1]) {@choices=(@choices,6)}$HINT=join(\'
\',@clues[@choices]); $CORRECT_HINT=join(\' \',@reinforcement[@choices]);$ORDERED=[@choices] '; ''
On a sheet of paper, draw a triangle. Label the vertices A, B, and C. Measure each side of your triangle.
AB = <_> inches
AC = <_> inches
BC = <_> inches
Which terms describe your triangle? ()
<_>  
equilateral isosceles scalene right obtuse acute